In Django 1.8
在Django 1.8中
class OtherModel(models.Model):
somefield = models.CharField(max_length=20)
class Orderform(models.Model):
sell_item_id = models.CharField(max_length=20)
class Selled(models.Model):
orderform = models.ForeignKey("Orderform")
sell_count = models.IntegerField()
something = OtherModel.objects.get(id=sell_item_id)
I need to use something like OtherModel.objects.get(id=sell_item_id)
.
我需要使用像OtherModel.objects.get(id = sell_item_id)这样的东西。
How to get sell_item_id
in class Selled(models.Model):
?
如何在类Selled(models.Model)中获得sell_item_id:?
2 个解决方案
#1
1
You schema couldn't be presented in SQL.
您的架构无法在SQL中显示。
Option #1:
选项1:
class Orderform(models.Model):
sell_item_id = models.CharField(max_length=20)
othermodel = models.OneToOneField("OtherModel")
and get it
得到它
Selled.objects.get(pk=1).orderform.othermodel
Option #2:
选项#2:
class Selled(models.Model):
orderform = models.ForeignKey("Orderform")
sell_count = models.IntegerField()
def something(self):
return OtherModel.objects.get(id=self.sell_item_id)
and get
得到
Selled.objects.get(pk=1).something()
But I think you should better think about you DB schema.
但我认为你应该更好地考虑一下DB模式。
#2
0
It looks like you have a couple of questions, for the first, to get the related
看起来你有几个问题,第一个是获得相关的问题
Selled.objects.filter(order_form__sell_item_id =id_to_get).select_related('order_form')
Notice the __
(double underscore) before sell_item_id
. This is important because it says, selected Selleed
by the sell_item_id
of the OrderForm
. and select_related
makes sure that order form is brought back in the results with a single call to the db.
请注意sell_item_id之前的__(双下划线)。这很重要,因为它说,由OrderForm的sell_item_id选择Selleed。和select_related确保通过单次调用db将订单返回到结果中。
Now, if you want to do that for OtherModel
, you will need to create a similar ForeignKey
field in the OtherNodel
and this will allow you to make the same query as above. Currently, you have no such relation.
现在,如果您想为OtherModel执行此操作,则需要在OtherNodel中创建类似的ForeignKey字段,这将允许您进行与上面相同的查询。目前,你没有这样的关系。
class OtherModel(models.Model):
somefield = models.CharField(max_length=20)
orderform = models.ForeignKey("Orderform")
OtherModel.objects.filter(order_form__sell_item_id =id_to_get).select_related('order_form')
Don't forget to run:
别忘了跑:
python manage.py makemigration
python manage.py migrate
This should solve the issue.
这应该可以解决问题。
#1
1
You schema couldn't be presented in SQL.
您的架构无法在SQL中显示。
Option #1:
选项1:
class Orderform(models.Model):
sell_item_id = models.CharField(max_length=20)
othermodel = models.OneToOneField("OtherModel")
and get it
得到它
Selled.objects.get(pk=1).orderform.othermodel
Option #2:
选项#2:
class Selled(models.Model):
orderform = models.ForeignKey("Orderform")
sell_count = models.IntegerField()
def something(self):
return OtherModel.objects.get(id=self.sell_item_id)
and get
得到
Selled.objects.get(pk=1).something()
But I think you should better think about you DB schema.
但我认为你应该更好地考虑一下DB模式。
#2
0
It looks like you have a couple of questions, for the first, to get the related
看起来你有几个问题,第一个是获得相关的问题
Selled.objects.filter(order_form__sell_item_id =id_to_get).select_related('order_form')
Notice the __
(double underscore) before sell_item_id
. This is important because it says, selected Selleed
by the sell_item_id
of the OrderForm
. and select_related
makes sure that order form is brought back in the results with a single call to the db.
请注意sell_item_id之前的__(双下划线)。这很重要,因为它说,由OrderForm的sell_item_id选择Selleed。和select_related确保通过单次调用db将订单返回到结果中。
Now, if you want to do that for OtherModel
, you will need to create a similar ForeignKey
field in the OtherNodel
and this will allow you to make the same query as above. Currently, you have no such relation.
现在,如果您想为OtherModel执行此操作,则需要在OtherNodel中创建类似的ForeignKey字段,这将允许您进行与上面相同的查询。目前,你没有这样的关系。
class OtherModel(models.Model):
somefield = models.CharField(max_length=20)
orderform = models.ForeignKey("Orderform")
OtherModel.objects.filter(order_form__sell_item_id =id_to_get).select_related('order_form')
Don't forget to run:
别忘了跑:
python manage.py makemigration
python manage.py migrate
This should solve the issue.
这应该可以解决问题。