I have 2 models :
class ModelA(Model):
_id = UUIDField(primary_key=True)
object_id = UUIDField()
object_version=IntegerField()
class ModelB(Model):
object_id = UUIDField()
meta = CharField(max_length=100)
_id Field in ModelA is pk. object_id and object_version will be unique together. But object_id remains same across different versions of ModelA instances.
Now ModelB has object_id which is referring (not through relations) to object_id field of ModelA. I don’t want to create ModelB instance for each version of ModelA and want to keep just non duplicate instance of ModelB for all version of ModelA.
In the queries I want to use meta field on each version of ModelA instance, but I am not able to find a way unless I create a relation to the ModelB. Is there a way to query ModelB fields in filter for queryset of ModelA using my current design?
Also FYI, there can be multiple modelB instances for a particular object_id.
One thing i could think of is to add m2m in my modelA but that would require me to fetch the modelB instance first which increases a db query while creation. Can anyone help
Hi Sarthak1996,
Can you clear up some confusion for me. You state:
I don’t want to create ModelB instance for each version of ModelA and want to keep just one instance of ModelB for all version of ModelA .
But then at the end indicate:
Also FYI, there can be multiple modelB instances for a particular object_id .
Those are contradictory given the information you’ve provided so far. Can you elaborate on those?
I’m considering suggesting creating a new model that’s only field would be object_id which would be unique. Then you can create ForeignKey or OneToOneField relationships to that model as needed.
Thanks,
Tim
@CodenameTim
Consider the following:
ModelA instances (using dummy values for clarity, in reality it would be UUID):
[
{_id:'uid1',object_id:'a',object_version:1},
{_id:'uid2',object_id:'a',object_version:2},
{_id:'uid3',object_id:'a',object_version:3},
{_id:'uid4',object_id:'a',object_version:4}
]
ModelB instances:
[
{id:1,object_id:'a',meta:'user_details'},
{id:2,object_id:'a',meta:'group_details'},
]
As you can see there are 2 instances of ModelB mapped to object_id:a of ModelA. Right now you see only 2 ModelB instances for 4 instances of ModelA.
What I meant by that is that not to create duplicate instances of ModelB for each instance of ModelA which have same object_id. Don’t want to add m2m as it increases a db query on create. All I want to do is access the ModelB instance while doing filter (like the way we do for relatedFields).
I’m considering suggesting creating a new model that’s only field would be object_id which would be unique.
I think this will not work in my case as there are multiple ModelB instances for one object_id.
Can’t use to_field in ForeignKey as the field should be unique
Given the additional information, I’m going to suggest you normalize your data model and move object_id into its own model.
class BaseObject(Model):
object_id = UUIDField(unique=True)
class ModelA(Model):
base = ForeignKey(BaseObject)
version = IntegerField()
class ModelB(Model):
base = ForeignKey(BaseObject)
meta = CharField()
Now you have the ability of find the related ModelA instances for a given ModelB instance and vice versa.
That’s brilliant 
Thanks a lot @CodenameTim
You’re welcome, best of luck!