I have three different models:
class Province(Model):
province = models.CharField(max_length=250)
class BaseCase(ModelWithStamps):
...
province = models.ForeignKey(Province, null=True, blank=True, on_delete=models.CASCADE)
class Event(BaseEvent):
....
@property
def province(self):
if hasattr(self, 'case'):
return self.case.province
return None
@property
def province_id(self):
if hasattr(self, 'case'):
return self.case.province.id
return None
And I have a serializer:
class BaseEditCaseSerializer(...):
....
province = serializers.ModelField(model_field=Case()._meta.get_field('province'),
required=False, allow_null=True)
class Meta:
model = ManualEvent
fields = (..., 'province')
Even, the province is a foreign key, the province field is displayed as a "text field" in front end and I cannot change it from there.
I want to display it as a dropdown (Province.objects.all()
). How can I do it?
from How to display a Foreign key serializer as a dropdown?
No comments:
Post a Comment