class MyBaseSerializer:
class Meta:
fields = (,)
exclude= (,)
class MySerializer(MyBaseSerializer):
class Meta:
// how to define fields/exclude to manipulate the fields/exclude defined in base serializer?
Since DRF says, it resembles django's form. I guess it could be done this way.. I'll try..
http://chriskief.com/2013/06/30/django-form-inheritance/
I know this issue is closed but i was just wondering what is the solution here ?
Inheriting the Meta class does not enable any field manipulation in the child. By the time the sub serializer is called the base Serializer already processed the fields.
This seems like a common case. I have a base model with control fields (id, created, modified, lat, lon , ip, user, group , client_timestamp etc ... ) and I would love to be able to create a corresponding base serializer class.
@hakib Sorry I didn't understand the question. Maybe a specific example?
Sure @tomchristie ,
class BaseModel(models.Model):
id = models.AutoField(primary_key=True)
created = models.DateTimeField(editable=False)
modified = models.DateTimeField()
client_time = models.DateTimeField()
user = models.ForeignKey(User)
class Inspection(BaseModel):
result = models.BooleanField()
The current use case i'm referring to is a backend for an app so when ever a resource is created i'm documenting the timestamps, the client timestamp for future investigation and the user. In the real implementation there are at least 4 more fields for location, ip etc and one or two related to permissions.
The corresponding serializes I would like to be able to create are simply
class BaseSerializer(serializers.Serializer):
class Meta:
fields = ('id','created','modified','client_time','user',)
read_only_fields = ('id', 'created', 'modified','user',)
write_only_fields = ('client_time',)
class InspectionSerializer(BaseSerializer):
class Meta(BaseSerializer.Meta):
model = Inspection
# This is what I would like to do ... obviously it's not working
fields += ('result')
I think this approach can help keep the serializers DRY (some would say on expense of being explicit...) .
If you really want you can do something like this...
class Meta(BaseSerializer.Meta):
model = Inspection
fields = BaseSerializer.Meta.fields + ('result',)
But that's about as far as our support for that style goes.
Figured as much, Thanks.
@tomchristie I have a related question. As far as I understand, 3.0 promotes us to use the extra_kwargs argument and specifying stuff as as dictionary:
class CreateUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('email', 'username', 'password')
extra_kwargs = {'password': {'write_only': True}}
But how usable is that with serializer meta inheritance?
As far as I understand, to override the inherited extra_kwargs I would need to perform a copy of the parent dictionary, update it, and set to the child's extra_kwargs (maybe something to add to the docs)?
But how usable is that with serializer meta inheritance?
Personal view on this is simply: who cares? Serializer meta inheritance just obscures the behavior you actually want. Forget about the nonsense idea that having as little code as possible is always best and just write the meta info explicitly.
If you really want it do something along the lines of:
extra_kwargs = dict(BaseSerializer.Meta.extra_kwargs.items() + {...}.items())
Or write a little wrapper function that lets you do
extra_kwargs = extend_kwargs(BaseSerializer.Meta, {})
But it's not a use case that I'm particularly sympathetic to us supporting.
I can extend the BaseSerialzier(serializers.ModelSerializer) with inheritance?
like the example:
class BaseSerializer(serializers.ModelSerialzier):
class Meta:
model = BaseModel
fields = ('id','created','modified','client_time','user',)
read_only_fields = ('id', 'created', 'modified','user',)
write_only_fields = ('client_time',)
class InspectionSerializer(BaseSerializer):
class Meta(BaseSerializer.Meta):
#model = Inspection
# This is what I would like to do ... obviously it's not working
fields += ('result')
extra_kwargs = dict(BaseSerializer.Meta.extra_kwargs.items() + {...}.items())
If anyone stumbles this way, since that last comment was made, python 3 lets you do this:
extra_kwargs = {**BaseSerializer.Meta.extra_kwargs, **{'my_kwargs': 'here'}}
馃憤
I was thinking about using a base Meta class for DRF serializers to set all fields to be read-only as in, and user inheritance to define exceptions:
class BaseMeta:
def __init__(*args , **kwargs):
self.read_only_fields = self.fields
BaseMeta.super(self, *args, **kwargs)
class PatchableMeta(BaseMeta):
PATCHABLE_FIELDS = ('some_patchable_field',)
def __init__(*args , **kwargs):
BaseMeta.super(self, *args, **kwargs)
self.read_only_fields -= PatchableMeta.PATCHABLE_FIELDS
I know that this is probably not the way Meta classes work, but is this doable?
Most helpful comment
If you really want you can do something like this...
But that's about as far as our support for that style goes.