Django:模型名称冲突 [英] Django: Model name clash

查看:393
本文介绍了Django:模型名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目中使用不同的开源应用程序。问题是,有两个不同的应用程序使用自己的模型定义使用相同的模型名称。



我尝试使用:

  class Meta:
db_table =db_name

但它没有起作用。我仍然在syncdb中得到字段名冲突错误。任何建议。



更新



我实际上正在尝试将Satchmo与Pinax 。错误是:


错误:一个或多个模型未验证:



contact.contact:字段'user'的访问者与相关的m2m字段'User.contact_set'冲突。在user的定义中添加一个related_name参数。



friends.contact:m2m字段'users'的访问者与相关字段User.contact_set冲突。在users的定义中添加一个related_name参数。


你是对的,表名已经是唯一的。我分析了模型,联系人模型分为两种不同的应用程序。当我评论这些模型之一,它的工作正常。



可能是错误是因为这两个应用程序都在PYTHON_PATH中,而当其他应用程序用相同名称定义其模型时,会发生冲突。

解决方案

问题是Satchmo和Pinax都有一个具有ForeignKey to User的联系人模型。 Django尝试为每个这些ForeignKeys的User添加一个contact_set反向关系属性,所以有一个冲突。



解决方案是添加像about_name = pinax_contact_set作为Pinax联系人模型中ForeignKey的参数,或类似于Satchmo Contact模型。这将需要直接为一个或另一个编辑源。您可能可以通过monkeypatch找到一种方法,但我希望这样做很棘手。


I am trying to use different open source apps in my project. Problem is that there is a same Model name used by two different apps with their own model definition.

I tried using:

    class Meta:
        db_table = "db_name"

but it didn't work. I am still getting field name clash error at syncdb. Any suggestions.

Update

I am actually trying to integrate Satchmo with Pinax. And the error is:

Error: One or more models did not validate:

contact.contact: Accessor for field 'user' clashes with related m2m field 'User.contact_set'. Add a related_name argument to the definition for 'user'.

friends.contact: Accessor for m2m field 'users' clashes with related field User.contact_set'. Add a related_name argument to the definition for 'users'.

You are right, table names are already unique. I analyzed the model and the Model 'Contact' is in two models of two different apps. When I comment out one of these models, it works fine.

May be the error is there because both apps are in PYTHON_PATH and when other app defines the its model with same name the clash occurs.

解决方案

The problem is that both Satchmo and Pinax have a Contact model with a ForeignKey to User. Django tries to add a "contact_set" reverse relationship attribute to User for each of those ForeignKeys, so there is a clash.

The solution is to add something like related_name="pinax_contact_set" as an argument to the ForeignKey in Pinax's Contact model, or similarly in the Satchmo Contact model. That will require editing the source directly for one or the other. You might be able to find a way to do it via monkeypatching, but I'd expect that to be tricky.

这篇关于Django:模型名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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