Using SlugRelatedField on a nullable FK

439 views
Skip to first unread message

steve-gregory

unread,
Jan 14, 2013, 2:28:12 PM1/14/13
to django-res...@googlegroups.com
Hello,

I would really like to use SlugRelatedField to describe a model with Django's User model as a foreign key by username, however the foreign key is blank=True, null=True.

I've tried setting required to False and default to N/A or just "" but each time I get the error:

'NoneType' object has no attribute 'username'

To be clearer, this is the model and serializer code:

class Tag(models.Model):
    name = models.SlugField(max_length=128)
    description = models.CharField(max_length=1024)                                                                                                                                 
    user = models.ForeignKey(User, null=True, blank=True)# Can be "User-Specific", or global                                                                                        
    def __unicode__(self):
        return "%s" % (self.name,)

class TagSerializer(serializers.ModelSerializer):                                                                                                                                   
    user = serializers.SlugRelatedField(slug_field='username', default="N/A", required=False)                                                                                                       
    class Meta:
        model = Tag

steve-gregory

unread,
Jan 14, 2013, 2:32:18 PM1/14/13
to django-res...@googlegroups.com
I also tried to use null=True but that also seemed to only accept None/"" as a value, and it was not built to handle relations that are None..

Thanks again
-Steve

Tom Christie

unread,
Jan 14, 2013, 5:17:32 PM1/14/13
to django-res...@googlegroups.com
Sounds like a valid bug to me.

If you have the time and you want to help out you might consider a pull request adding some tests for slug relation fields. (which are currently missing)  It's easy enough to do, because there are already `relations_hyperlink.py` and `relations_pk.py` test modules, that exactly mirror each other, and we just need to add a `relations_slug.py` that does the same.  (Yes at some point there may be some refactoring.)

A pull request for those tests would be really ace (including presumably, nullable tests that currently fail), that way I can concentrate on getting a fix sorted, but if you don't have the time no problem - the bug report is still appreciated.

I've created a ticket for this here: https://github.com/tomchristie/django-rest-framework/issues/580

Cheers!

  Tom

steve-gregory

unread,
Jan 15, 2013, 3:48:30 PM1/15/13
to django-res...@googlegroups.com
Tom,

Thanks for the quick reply. I've written some test cases that fail using the same logic that was presented in relations.py and relations_hyperlink.py.

I created four different slug serialzers, one with default, required, null and without any other args than the slug_name, all four fail at various points and I wrote comments in the code as to what the failure is and what I believe is expected.

If you have any changes you would like me to make let me know here or via the pull request form on github.

-Steve Gregory
Reply all
Reply to author
Forward
0 new messages