I'm writing a Django app using django==1.10
and djangorestframework==3.4.6
Please consider the following code:
I have two models:
class BaselineModel(models.Model):
subject = models.ForeignKey('custom_auth.User', blank=True)
weight = models.SmallIntegerField(null=True, blank=True)
class DosageModel(models.Model):
subject = models.ForeignKey('custom_auth.User', blank=True)
udca = models.SmallIntegerField(null=True, blank=True)
weight = models.SmallIntegerField(null=True, blank=True)
And a Serializer:
class DosageSerializer(serializers.ModelSerializer):
class Meta:
exclude = ("subject",)
model = DosageModel
Question: How can I set the initial value for DosageModel.weight
to BaselineModel.weight
?
Use Case: Weights initial value will be the weight registered at Baseline. The user has the option to overwrite the initial value.
Edit: The initial value should be "initial", meaning it should be displayed i the HTML input field before the data object is created.
You can try the following
But you will run into a problem: The Default value will be fetched once at the start of the server.
This will fetch the value every time the Model is initialized. This works with ModelForms and I am not sure if the RestSerializers behave the same way.