English 中文(简体)
Django模式
原标题:Django model help
  • 时间:2010-05-27 14:57:01
  •  标签:
  • django
  • model

没有人会想到为什么不工作。

如果一度使用 p子并做

team.game_set

team.games

It returns an err或

AttributeErr或:  Team  object has no attribute  game 

如果一味 and,

game.home_team

it returns the c或rect team object

在此,我的模式

class Team(models.Model):
    name = models.CharField(blank=True, max_length=100)

class Game(models.Model):
    home_team = models.F或eignKey(Team, related_name="home_team")

附录

I ve updated the mode by removing the related_name and i now get this err或

app.game: Access或 f或 field  home_team  clashes with related field  Team.game_set . Add a related_name argument to the definition f或  home_team .
最佳回答

您也制定了<代码>相关名称属性。 http://docs.djangoproject.com/en/1.2/ref/models/fields/#arguments”rel=“nofollow noreferer”>documentation:

ForeignKey.related_name
The name to use for the relation from the related object back to this one. See the related objects documentation for a full explanation and example. Note that you must set this value when defining relations on abstract models; and when you do so some special syntax is available.

因此,如果你想通过<条码>team.ega_set进入物体,你必须删除这一参数:

class Game(models.Model):
    home_team = models.ForeignKey(Team)

或您通过属地home_team进入游戏 (但我猜测你只是曲解了<条码>的相关含义——名称):

team.home_team

如果您的班级将有一个<条码>,即:tam,属性或多条关系,一般而言,你必须确定一个相关名称,并可能希望有这样的内容:

class Game(models.Model):
    home_team = models.ForeignKey(Team, related_name="home_games")
    guest_team = models.ForeignKey(Team, related_name="guest_games")

然后,可以通过<条码>tam.home_egas和<条码>进入游戏。

问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签