English 中文(简体)
django
原标题:django ManyToMany through help

Hay I ve 谈到关系问题。

我想让用户有友谊。 因此,用户可以成为另一个用户的朋友。 我假定,需要通过一个友谊桌来使用许多“ToMany”现场。 但我们不能让它发挥作用。 任何想法?

这里是我的榜样。

class User(models.Model):
    username = models.CharField(max_length=999)
    password = models.CharField(max_length=999)
    created_on = models.DateField(auto_now = False, auto_now_add = True)
    updated_on = models.DateField(auto_now = True, auto_now_add = False)
    friends = models.ManyToManyField( self )
    pendingFriends = models.ManyToManyField( self , through= PendingFriendship , symmetrical=False, related_name= friend_requested )

class PendingFriendship(models.Model):
    user = models.ForeignKey( User , related_name= user )
    requested_friend = models.ForeignKey( User , related_name= requested_friend )
    created_on = models.DateField(auto_now = False, auto_now_add = True)
    updated_on = models.DateField(auto_now = True, auto_now_add = False)

增 编

最佳回答

http://docs.djangoproject.com/en/1.1/ref/models/fields/#manytomanyfield”rel=“noreferer”>。 赞成:

class User(models.Model):
    username = models.CharField(max_length=999)
    password = models.CharField(max_length=999)
    created_on = models.DateField(auto_now = False, auto_now_add = True)
    updated_on = models.DateField(auto_now = True, auto_now_add = False)
    friends = models.ManyToManyField( self )

如果你想在关系中增加额外领域,你只得使用<条码>。 关于所有可能性的另一种描述见http://docs.djangoproject.com/en/1.1/topics/db/models/#many-to-many-relations”rel=“noreferer”>。

您称“你”的模型User,我假定你没有在认证框架内使用该建筑。 但是,如果你使用,你就不必执行认证,这样就想。

Update: Have you read the sections I linked to? There it is described:

对中间模式有一些限制:

  • Your intermediate model must contain one - and only one - foreign key to the target model (this would be Person in our example). If you have more than one foreign key, a validation error will be raised.
  • Your intermediate model must contain one - and only one - foreign key to the source model (this would be Group in our example). If you have more than one foreign key, a validation error will be raised.
  • The only exception to this is a model which has a many-to-many relationship to itself, through an intermediary model. In this case, two foreign keys to the same model are permitted, but they will be treated as the two (different) sides of the many-to-many relation.
  • When defining a many-to-many relationship from a model to itself, using an intermediary model, you must use symmetrical=False (see the model field reference).
问题回答

这一博客解释了你需要什么。 从博客中,你只得改变相关名称。

pendingFriends = models.ManyToManyField( self , through= PendingFriendship , symmetrical=False, related_name= friend_requested )

页: 1

related_name= friend_requested+ 

然而,我仍然无法自动建立不对称关系。





相关问题
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 ...

热门标签