如何告诉Django不要为M2M相关领域创建一个表? [英] How do I tell Django to not create a table for an M2M related field?

查看:94
本文介绍了如何告诉Django不要为M2M相关领域创建一个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Django代码片段的这个小宝石从两个方向编辑ManyToManyField:

  class ManyToManyField_NoSyncdb 
def __init __(self,* args,** kwargs):
super(ManyToManyField_NoSyncdb,self).__ init __(* args,** kwargs)
self.creates_table =

$ b class Job(model.Model):
persons = ManyToManyField_NoSyncdb(Person,blank = True,db_table ='person_jobs')
pre>

(片段详情 here



它允许我从工作表单中选择给定工作中的所有人员,并反向让我从个人表单中选择一个人的所有工作,并更新单个jobs_persons表在这两种情况下,



然而,从Django 1.0移动到1.2时,syncdb现在会生成一个重复的表错误,因为created_table显然不再是支持d属性在基类。



有没有另外一种方法可以指示Django 1.2不为相关框架创建表?

解决方案

所以,如果你想在管理员的两个模型中访问ManyToMany,那么官方解决方案是使用 inlinemodel 。我也有同样的问题/只需要几天前。而且我并不满足于 inlinemodel 解决方案(如果你有很多条目,数据库查询很重,不能使用 filter_horizo​​ntal 小部件等这个解决方案是这样的:这个解决方案是这样的:(Django 1.2+和 syncdb )的解决方案是这样的:





  class User(models.Model):
groups = models.ManyToManyField('Group',through ='UserGroups'

class Group(model.Model):
users = models.ManyToManyField('User',through ='UserGroups')

class UserGroups(models.Model )
user_id = models.ForeignKey(User)
group_id = models.ForeignKey(Group)

class Meta:
db_table ='app_user_group'
auto_created =用户

查看票 897 获取更多信息。



不幸的是,如果您使用 South ,您将不得不删除在自动创建的每个迁移文件中创建 app_user_group 表。


I'm using this little jewel of a Django code snippet to edit a ManyToManyField from both directions:

class ManyToManyField_NoSyncdb(models.ManyToManyField):  
    def __init__(self, *args, **kwargs):  
        super(ManyToManyField_NoSyncdb, self).__init__(*args, **kwargs)
        self.creates_table = False  

class Job(models.Model):  
    persons = ManyToManyField_NoSyncdb( Person, blank=True, db_table='person_jobs' )

(snippet details here)

It lets me select all the persons in a given job from the jobs form and inversely lets me select all the jobs for a person from the persons form and updates the single jobs_persons table in both cases.

Upon moving from Django 1.0 to 1.2, however, syncdb now generates a duplicate table error because creates_table is evidently no longer a supported property in the base class.

Is there another way to instruct Django 1.2 not to create a table for a RelatedField?

解决方案

So, if you want to have access to the ManyToMany in both models in the Admin, currently the official solution is to use inlinemodel for the second model. I had also this same problem/need just a few days ago. And I was not really satisfied with the inlinemodel solution (heavy in DB queries if you have a lot of entries, cannot use the filter_horizontal widget, etc.).

The solution I found (that's working with Django 1.2+ and syncdb) is this:

class User(models.Model):
    groups = models.ManyToManyField('Group', through='UserGroups')

class Group(models.Model):
    users = models.ManyToManyField('User', through='UserGroups')

class UserGroups(models.Model):
    user_id = models.ForeignKey(User)
    group_id = models.ForeignKey(Group)

    class Meta:
        db_table = 'app_user_group'
        auto_created = User

See ticket 897 for more info.

Unfortunately, if you're using South you will have to remove the creation of the app_user_group table in every migration file created automatically.

这篇关于如何告诉Django不要为M2M相关领域创建一个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆