Answer the question
In order to leave comments, you need to log in
How to make an additional selection condition for a foreign key field in a django model?
There are 2 models:
class A(models.Model):
id = models.PositiveIntegerField(default=0, primary_key=True)
version = models.PositiveIntegerField(default=0)
value = models.PositiveIntegerField(default=0)
class B(models.Model):
id = models.PositiveIntegerField(default=0, primary_key=True)
version = models.PositiveIntegerField(default=0)
a_value = models.ForeignKey(A, on_delete=models.DO_NOTHING)
A.objects.get(pk=self.a_value)
A.objects.get(pk=self.a_value, version=self.version)
Answer the question
In order to leave comments, you need to log in
In order not to constantly register, create a custom queryset manager and make a method there in which the version field will be taken into account
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question