-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Raise error when ModelSerializer used with abstract model. #2630
Comments
I don't think that you should be able to do that. An abstract model is just a Python class for sharing code with other models, it's not actually a model, so here you're referencing to a serializer which is backed by a model that doesn't actually exist. |
I'm not sure why you need it it to be an abstract model, but even in this set-up you can reference it as a single field. Or if you really need a separate model for the full name, make a it one-to-one relation with |
The one to one model will end up create another table which I might not want. |
I don't think this has been sufficiently narrowed down to be treated as an issue. As a side note: almost any issue that comes to us with
|
Here is a more compact example: class Name(models.Model):
first = models.CharField(max_length=255)
class Meta:
abstract = True
class NameSerializer(serializers.ModelSerializer):
class Meta:
model = Name trying to print
|
The issue appears to be an assumption that a model has a primary key. Specifically |
Okay, that's much better - retitled the issue. |
Given that the |
Well technically it's not a model (in the common sense of the word), but just a Python class, which is why it's probably not ModelSerializer's domain. |
Acceptable resolutions:
First def sounds preferable to me. |
As a side note for conscience, does anyone know if Django's |
@maryokhin It doesn't. Anyways decision done - raise a big loud error if ModelSerializer is used with an abstract model. Use a regular Serializer if you have a valid reason to want something that maps to an abstract model class. |
Retitled the issue given the latest assessment. |
In thinking about this more, it really would be cool to have some way of generating a Serializer from an abstract Model. Perhaps the name of this Serializer should not be class FullNameSerializer(serializers.Serializer):
name_first = serializers.CharField(allow_blank=True, max_length=MAX_LENGTH_NAME_FIELDS, required=False)
name_middle = serializers.CharField(allow_blank=True, max_length=MAX_LENGTH_NAME_FIELDS, required=False)
name_last = serializers.CharField(allow_blank=True, max_length=MAX_LENGTH_NAME_FIELDS, required=False) I lose the nice fact that the |
I am going to have a look |
Hi, first of all thanks for the great work. This is my first time I work with DRF and run right into this issue. I agree with cancan101 and think it still makes sense for DRY reasons. Or maybe there's a better way for doing this? |
Given that it currently doesn't work, we should in either case make sure that it currently loudly raises an error. We could reconsider this if someone supplies a patch that does make it work with abstract models. DRY is a poor tradeoff if it means introducing too much implicit, hidden behavior. |
okay |
With:
I am trying:
but I get:
The text was updated successfully, but these errors were encountered: