我有以下课程:成分,食谱和食谱内容......
class Ingredient(models.Model):
name = models.CharField(max_length=30, primary_key=True)
qty_on_stock = models.IntegerField()
def __unicode__(self):
return self.name
class Recipe(models.Model):
name = models.CharField(max_length=30, primary_key=True)
comments = models.TextField(blank=True)
ingredient = models.ManyToManyField(Ingredient)
def __unicode__(self):
return self.name
class RecipeContent(models.Model):
recipe = models.ForeignKey(Recipe)
ingredients = models.ForeignKey(Ingredient)
qty_used = models.IntegerField()
Run Code Online (Sandbox Code Playgroud)
但是对于RecipeContent中的__unicode __(),我想使用这个RecipeContent所属的Recipe名称...有没有办法做到这一点?
对于:
app/
__init__.py
abc.py
mod/
__init__.py
def.py
Run Code Online (Sandbox Code Playgroud)
我如何从def.py导入abc.py?
我有一个人类,可以有几个家庭,每个家庭有一个或多个电话号码.
我已经定义了这些类,但现在我正在尝试创建一个列出每个人的视图,包括所有家庭和每个家庭地址的所有电话号码......类似于:
john smith
123 fake str
305-99-8877
305-99-8876
321 oak road
444-98-7654
peter guy
453 north ave...
Run Code Online (Sandbox Code Playgroud)
到目前为止我有这样的事情:
(在我的views.py上)
def ViewAll(request):
people = Person.objects.all()
render_to_response('viewall.html', {'people': people})
Run Code Online (Sandbox Code Playgroud)
(在我的模板上)
{% for guy in people %}
{{ guy.name }}
{% if person.home_address_set.all %}
{{ home_address }}
{% for ?????? in ???? %}
#print phone numbers in each home
{% endfor %}
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
任何关于如何写我错过的想法?当然,如果有另一种方式(一种更优雅或更有效的方式)做我需要的东西,我很乐意听到它.