Hello,
I’m a relative Django n00b so apologies if this is a novice question.
I have 2 entities:
- EntityOne
- EntityTwo, which has a OneToOneField mapping with EntityOne
My view has a put()
method for updates:
class MyView(...):
def put(self, request, *args, **kwargs):
serializer = EntityOneUpdateRequestSerializer(instance, request.data)
if serializer.is_valid():
...
The first serializer is as follows:
class EntityOneUpdateRequestSerializer(...):
class Meta:
fields = ("entity_one",)
entity_one = EntityOneUpdateSerializer()
The second (nested) serializer is as follows:
class EntityOneUpdateSerializer(...):
class Meta:
model = models.EntityOne
fields = (
...
"entity_two",
...
)
entity_two = EntityTwoUpdateSerializer(...)
EntityTwoUpdateSerializer
has a validate()
method where I need to compare the values of EntityTwo that have changed with the current instance - the current one in the database.
class EntityTwoUpdateSerializer(...):
model = models.EntityTwo
def validate(self, data):
...
How do I get the current instance to compare against the proposed changed values? I’ve tried referencing self.instance
in the validate()
method, only to find that it is not supplied when the function is called.
I’ve also tried using the context = {}
keyword but when I get to the definition of the EntityTwoUpdateSerializer
in EntityOneUpdateSerializer
, I do not have a self
object that I can get it from.
Many thanks in advance for any assistance.