I have two models:
class Foo(models.Model):
foo_field = ...
class Bar(models.Model):
foo = models.ForeignKey(Foo, on_delete=models.CASCADE)
bar_field = ...
and I can access all Bar instances related to a Foo with:
Foo.bar_set.all()
Is there a way to change the 'reverse name', like in ManyToManyField, so that I can write:
Foo.bars.all()
?
yes, using related_name
class Foo(models.Model):
foo_field = ...
class Bar(models.Model):
foo = models.ForeignKey(Foo, related_name="bars", on_delete=models.CASCADE)
bar_field = ...
Related
class a:
def init(self,a:int):
assert a>0 ,"a is less than zero"
class b(a):
def init(self,a):
self.a=a
a.init(self,a)
m=b(-8)
Blockquote
class ASerializer(serializers.ModelSerializer):
b = Bserializer()
...
class BSerializer(serializers.ModelSerializer):
...
I need to call BSerializer before it is defined. How do I do that?
class ASerializer(serializers.ModelSerializer):
def __init__(self, instance=None, **kwargs):
super().__init__(instance, **kwargs)
self.fields['b'] = BSerializer()
class BSerializer(serializers.ModelSerializer):
...
I have the following class:
class ClassA(Object):
VERSIONS = {
'1': {
'x': a.b.x.X # where x is the module and X is the class
}
}
ATTRS = ['y', 'z']
def __init__(self, **kwargs):
...
do_something...
...
for attr in ATTRS:
setattr(self, attr, VERSIONS[ver][attr]())
I am using the above code as follows:
class_a = ClassA()
class_a.x.y
where x is a module and y is the method in that module
I need to mock this call now. How do I mock it, since the method y is not part of ClassA?
This can be done as follows:
mock_class_a.return_value.x.y = value_to_return
Is there a way to declare this case so that it works? I hope the code is self-explanatory.
class A(Model):
many_to_one = models.ForeignKey(B)
(...)
class B(A):
(...)
class A(Model):
many_to_one = models.ForeignKey('B')
(...)
class B(A):
(...)
Considering the class model as follows:
alt text http://www.forteresse.net/site/stackoverflow/classes.png/image
How do you do this in models.py?
class House(models.Model):
foo = models.CharField(max_length=123)
class Caravan(models.Model):
foo = models.CharField(max_length=123)
class Door(models.Model):
bar = models.CharField(max_length=123)
house = models.ForeignKey(House)
caravan = models.ForeignKey(Caravan)
But these foreign key definitions may not be what is intended. How do you code this in django? The intention is to reuse the same model "Door" for both "House" and "Caravan".
After digging deeper, I found this; is this the right way to model the problem?
class House(models.Model):
foo = models.CharField(max_length=123)
class Caravan(models.Model):
foo = models.CharField(max_length=123)
class Door(models.Model):
bar = models.CharField(max_length=123)
house = models.ForeignKey(House, null=True, blank=True)
caravan = models.ForeignKey(Caravan, null=True, blank=True)
class Door(models.Model):
bar = models.CharField(max_length=123)
class Caravan(models.Model):
foo = models.CharField(max_length=123)
doors = models.ManyToManyField(Door)
class House(models.Model):
foo = models.CharField(max_length=123)
doors = models.ManyToManyField(Door)
I think you should try:
class Door(models.Model):
bar = models.CharField(max_length=123)
class House(models.Model):
foo = models.CharField(max_length=123)
door = models.ForeignKey(Door)
class Caravan(models.Model):
foo = models.CharField(max_length=123)
door = models.ForeignKey(Door)
Can you clarify what you're really looking for with an example query? It's not clear to me. This is what I think you're looking for:
class Door(models.Model):
bar = models.CharField(max_length=123)
class House(Door):
foo = models.CharField(max_length=123)
class Caravan(Door):
foo = models.CharField(max_length=123)
Then you can do things like Caravan.objects.values('foo','bar')