Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metaclass attribute depth ignores fields attribute #2287

Closed
PhilipGarnero opened this issue Dec 17, 2014 · 2 comments
Closed

Metaclass attribute depth ignores fields attribute #2287

PhilipGarnero opened this issue Dec 17, 2014 · 2 comments
Labels
Milestone

Comments

@PhilipGarnero
Copy link
Contributor

@PhilipGarnero PhilipGarnero commented Dec 17, 2014

Hello !
I'm having some trouble using nested serialization with drf 3.0.1:
I found out that the depth argument ignores what is set in the fields argument and if I remove it,
I can't have any nested serialization.
I'm a bit stuck at this point, if I remove it I lose the nested serialization but if I let it, it will put everything it encounters in the nested until throwing errors like AssertionError: May not set both 'read_only' and 'required'.
Here's my code :

#models.py
class Profile(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=80)
    avatar = models.ImageField(storage=protected_media, upload_to=to_user_dir)


class Adventure(models.Model):
    name = models.CharField(max_length=50)
    user_profile = models.ForeignKey(Profile)

#serializers.py
class ProfileSerializer(serializers.HyperlinkedModelSerializer):
    avatar = serializers.ImageField(max_length=None, allow_empty_file=False)

    class Meta:
        model = Profile
        fields = ("url", "avatar", "name") #user field is not present

class NestedAdventureSerializer(serializers.HyperlinkedModelSerializer):
    levels = NestedLevelSerializer(source="level_set"), #some uninteresting serializer
    nested_url = serializers.HyperlinkedIdentityField(view_name='nested_adventure-detail')
    url = serializers.HyperlinkedIdentityField(view_name='adventure-detail')

    class Meta:
        model = Adventure
        fields = ("url", "nested_url", "name", "level_set") #user_profile field is not present
        depth = 2

Here's what I get when I print a NestedAdventureSerializer's instance:

NestedSerializer(read_only=True, required=True):
    url = HyperlinkedIdentityField(view_name='contenttype-detail')
    name = CharField(max_length=100)
    app_label = CharField(max_length=100, required=True)
    model = CharField(label='Python model class name', max_length=100, required=True)
    class Meta:
        validators = [<UniqueTogetherValidator(queryset=ContentType.objects.all(), fields=(u'app_label', u'model'))>] None
NestedSerializer(read_only=True, required=True):
    url = HyperlinkedIdentityField(view_name='contenttype-detail')
    name = CharField(max_length=100)
    app_label = CharField(max_length=100, required=True)
    model = CharField(label='Python model class name', max_length=100, required=True)
    class Meta:
        validators = [<UniqueTogetherValidator(queryset=ContentType.objects.all(), fields=(u'app_label', u'model'))>] None
NestedSerializer(read_only=True, required=True):
    url = HyperlinkedIdentityField(view_name='adventure-detail')
    name = CharField(max_length=50)
    user_profile = NestedSerializer(read_only=True):
        url = HyperlinkedIdentityField(view_name='profile-detail')
        name = CharField(max_length=80)
        avatar = ImageField(max_length=100)
        user = NestedSerializer(read_only=True):
             # and it goes on with django's User model until throwing assertions
NestedAdventureSerializer():
    url = HyperlinkedIdentityField(view_name='adventure-detail')
    nested_url = HyperlinkedIdentityField(view_name='nested_adventure-detail')
    name = CharField(max_length=50)
    level_set = ListSerializer(child=NestedSerializer(read_only=True):, read_only=True)

Thank you in advance for your help !

@tomchristie
Copy link
Member

@tomchristie tomchristie commented Dec 18, 2014

Hi @PhilipGarnero, thanks for raising the issue.

I'm going to be slightly unhelpful at this point and close the issue because it's not sufficiently broken down into an easily actionable ticket - there's simply too much stuff there that's specific to your application and I can't really see what I'm supposed to be looking at.

What I'd suggest is that you break this down to the simplest possible case that you can find that demonstrates the issue your seeing. (Eg. a couple of models each with one or two fields.) That process will help nail down if it's actually a bug requiring action, or a usage issue.

Once you've got a simple reviewable example I'd be very happy to reopen the ticket and give it further consideration.

Apologies for not being more help at this point, but we do need to see tickets presented in a way that makes it easy for us to assess if they are valid issues or not.

Thanks for your understanding.

@tomchristie
Copy link
Member

@tomchristie tomchristie commented Dec 19, 2014

@tomchristie tomchristie reopened this Dec 19, 2014
@tomchristie tomchristie added this to the 3.0.3 Release milestone Dec 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants