I have two models:
class ModelOne(models.Model):
something = models.TextField[...]
class ModelTwo(models.Model):
other_something = models.TextField[...]
ref = models.ForeignKey(ModelOne)
And I want to write function in ModelOne which return me all related objects from ModelTwo.
It's important: In ModelOne.
How to do it?
Invoke self.modeltwo_set.all().
Related
I have a simple ForeignKey relationship:
class Foo(models.Model):
id = UUIDField()
class Bar(models.Model):
id = UUIDField()
foo = ForeignKey(foo)
If I have an initial queryset of Bar objects, how can I get a queryset of related Foo object for each respective Bar?
I'm currently doing this but I'm wondering if there's a better way:
bar_qs = Bar.objects.all().select_related("foo")
foo_ids = []
for i in bar_qs:
foo_ids.append(i.foo.id)
foo_qs = Foo.objects.filter(id__in=foo_ids)
Try this query:
Foo.objects.filter(bar_set__in=Bar.objects.all())
You're doing the right thing in using select_related because that gathers the related foo object in the same query so there's no need to query the database again for the Foo table.
If a list of foos is ok, you could just do;
bar_qs = Bar.objects.all().select_related("foo")
foos = []
for i in bar_qs:
foos.append(i.foo)
Or you could just start with the foo queryset using;
foo_qs = Bar.objects.select_related('foo').only('foo')
But if you need a bar_qs, then you'd be best doing;
bar_qs = Bar.objects.all().select_related("foo")
foo_qs = bar_qs.only('foo')
Assume, we have model
class BaseModel(models.Model):
is_a = models.BooleanField()
and two models related to this one:
class A(models.Model):
value_1 = models.IntegerField()
base = models.ForeignKey(BaseModel, related_name='a')
class B(models.Model):
value_1 = models.IntegerField()
value_2 = models.IntegerField()
base = models.ForeignKey(BaseModel, related_name='b')
What I need is to refer to A or B depending on is_a property.
For example,
base = BaseModel.objects.get(id=1)
if base.is_a:
obj = A.objects.create(value_1=1, base=base)
else:
obj = B.objects.create(value_1=1, value_2=2, base=base)
return obj
or
if base.is_a:
queryset = base.a.all()
else:
queryset = base.b.all()
return queryset
i.e., every time I have to check the is_a property.
Is there more graceful way?
There are two only related models, A and B, no other ones will appear in the nearest future.
Part of the problem can be solved with django-polymorphic, e.g.:
class A(PolymorphicModel):
...
class B(A):
...
This allows to retrieve all A's and B's with one request like base.b.all(), but the problem here is that every B creates instance of A, which is unwanted.
I've considered GenericForeignKey as well. As far as I understood it has a number of limitations like "1) You can't use GenericForeignKey in query filters ; 2) a GenericForeignKey won't appear in a ModelForm" (from GenericForeignKey or ForeignKey).
One idea is to add choices to the BaseModel to have a string representation of your boolean value. If you set the strings equal to the A and B model names, you can use the model.get_foo_display() method to return the name of the model. Then use the Python getattr() method to access attributes as variables.
class BaseModel(models.Model):
base_model_choices = (
(True, 'A'),
(False, 'B'),
)
is_a = models.BooleanField(choices=base_model_choices)
For example,
base = BaseModel.objects.get(id=1)
queryset = base.getattr(models, get_is_a_display()).all()
obj = getattr(models, get_is_a_display()).objects.create(base=base)
i need to join a table without a direct relationship.
models.py:
class FooModel():
bar = ForeignKey(Bar)
class BarModel():
pass
class BazModel():
bar = ForeignKey(Bar)
class QuxModel():
foo = ForeignKey(Foo)
tried to reach Foo from Baz but didn't work
viewset.py:
def BazView(viewsets.ModelViewSet):
queryset = model.BazModel.objects.all().prefetch_related('bar').prefetch_related('baz__bar')
serializer_class = serializer.Baz
def get_queryset(self):
return self.queryset
serializer.py
class FooSerializer(serializer.ModelSerializer):
class Meta:
model = FooModel
exlude = []
class BarSerializer(serializer.ModelSerializer):
class Meta:
model = BarModel
exlude = []
class BazSerializer(serializer.ModelSerializer):
foo = FooSerializer()
class Meta:
model = BarModel
exlude = []
class QuxSerializer(serializer.ModelSerializer):
class Meta:
model = QuxModel
exlude = []
using prefetch like that i got an error saying that Baz has no foo field.
also would like to get data from QuxModel based on Foo FK...
how could i perform this?
Try this:
model.BazModel.objects.all().select_related('bar').prefetch_related('bar__foomodel_set')
And then in BazSerializer, you can set the source for foo like this:
class BazSerializer(serializer.ModelSerializer):
foo = FooSerializer(source='bar.foomodel_set')
This will tell the foo serializer to get it's data from the FooModel objects using BazModel's bar.
You might have to change foomodel_set to the related name you have set to access FooModel from BarModel.
Is it possible to do something like this?
class Doc1:
fieldd1 = StringField()
class Doc2:
fieldd2 = ReferenceField(Doc1.fieldd1)
Or should I just reference the Doc and then get the field information whenever I need it
This not posible and it is reference to document. To get fieldd1 you must do:
class Doc1(Document):
fieldd1 = StringField()
class Doc2(Document):
fieldd2 = ReferenceField(Doc1)
Doc2.objects.first().fieldd2.fieldd1
If you want just include document to another as part of one document then look at EmbeddedDocument and EmbeddedDcoumentField:
class Doc1(EmbeddedDocument):
fieldd1 = StringField()
class Doc2(Document):
fieldd2 = EmbeddedDcoumentField(Doc1)
Doc2.objects.first().fieldd2.fieldd1
But you always can set own properties:
class Doc1(Document):
fieldd1 = StringField()
class Doc2(Document):
fieldd2 = ReferenceField(Doc1)
#property
def fieldd1(self):
return self.fieldd2.fieldd1
Doc2.objects.first().fieldd1
See documentation: https://mongoengine-odm.readthedocs.org/en/latest/guide/defining-documents.html.
Is it possible to instantiate a subclassed model from its parent?
class Object1(models.Model):
field1a = models.CharField()
field1b = models.CharField()
feild1c = models.ForeignKey(Object4)
class Object2(Object1):
field3 = models.CharField()
class Object3(Object1):
field3 = models.CharField()
class Object4(models.Model):
field4 = models.CharField()
What I want to do is create the base class first and then based on some rule instantiate one of the subclasses but using the already created base class.
Something like:
obj4 = Object4(field4='d')
obj1 = Object1(field1a='a', field1b='b', field1c=obj4)
if somerule:
obj2 = Object2(object1_ptr=obj1, field2='2')
else:
obj3 = Object3(object1_ptr=obj1, field3='3')
I don't want to repeat the Object1 fields in the if/else clauses. Is it possible to accomplish this? When I try this I get a Foreign key error;
Cannot add or update a child row: A foreign key constraint fails
I recommend doing something like this:
attr = dict(field1a='a', field1b='b', field1c=obj4)
obj1 = Object1(**attr)
if somerule:
attr["field2"] = 2
obj2 = Object2(**attr)
else:
attr["field3"]='3'
obj3 = Object3(**attr)
Be aware that the dictionary attr changes in place.
What you're doing is almost correct, but if you want to copy it you will have to remove the primary key.
So... this should fix it: del obj2.id
Do note however that if some other model references your model with a foreign key that it references obj1, not obj2. And obj1 will, ofcourse, still exist.