我的模特
class BaseModel(models.Model):
CreatedDate = models.DateTimeField(auto_now_add=True, verbose_name="Oluşturulma Tarihi")
ModifiedDate = models.DateTimeField(auto_now=True, verbose_name="Son Güncellenme tarihi")
Isdeleted = models.BooleanField(verbose_name="Silindi", default=False)
class Case(BaseModel):
CaseNumber = models.CharField(max_length=14)
Customer = models.ForeignKey(Customer)
Title = models.ForeignKey(CaseTitle)
CaseCategory = models.ForeignKey(CaseCategory, verbose_name="Kategori")
Priority = models.ForeignKey(CasePriority)
Status = models.ForeignKey(CaseStatus)
Detail = models.TextField()
Group = models.ForeignKey(Group)
User = models.ForeignKey(User,related_name='User' )
AssignedUser = models.ForeignKey(User,related_name='AssignedUser')
CloseDetail = models.TextField blank=True)
我只是想给我的模型提供2个外键,但错误是
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/models/fields/related.py", line 796, in __init__
to._meta.model_name AttributeError: 'ForeignKey' object has no attribute '_meta'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 338, in execute
django.setup()
File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/code/Case/models.py", line 91, in <module>
class Case(BaseModel):
File "/code/Case/models.py", line 101, in Case
AssignedUser = models.ForeignKey(User,related_name='AssignedUser')
File "/usr/local/lib/python3.6/site-packages/django/db/models/fields/related.py", line 802, in __init__
RECURSIVE_RELATIONSHIP_CONSTANT, AssertionError: ForeignKey(<django.db.models.fields.related.ForeignKey>) is invalid.
First parameter to ForeignKey must be either a model, a model name, or the string 'self'
答案 0 :(得分:1)
将字段名称User更改为您想要的任何内容。这与您收到错误的原因相冲突。
class MultiCoupon {
public static function getChildCouponByMainCouponCode($main_coupon_code, $provider_condition) {
global $sql_tbl;
$child_discount_coupon_data = func_query_first("SELECT child_coupons.* FROM $sql_tbl[discount_coupons] as child_coupons INNER JOIN $sql_tbl[discount_coupons_links] as main_coupons ON child_coupons.main_code = main_coupons.code AND main_coupons.code='" . addslashes($main_coupon_code) . "' $provider_condition");
return $child_discount_coupon_data;
}
}
答案 1 :(得分:0)
如果只与模型User
实现一个关系,它是否有效?错误消息:
First parameter to ForeignKey must be either a model, a model name, or the string 'self'
让我假设您没有传递User
模型的实际类。你是如何获得它的?
另外,通过查看我认为BaseModel
的字段,它可能是一个抽象的基本模型:
class BaseModel(models.Model):
class Meta:
abstract = True
CreatedDate = models.DateTimeField(auto_now_add=True, verbose_name="Oluşturulma Tarihi")
...
有关详细信息,请参阅此处:https://godjango.com/blog/django-abstract-base-class-model-inheritance/
答案 2 :(得分:0)
您正在重新定义User
Django代码的相关部分是isinstance(to, str)
,这意味着User
不是模型,但它也不是一个字符串(&#39; sa ForeignKey
)。
如果您正在使用Django的用户模型或自定义变体,那么单独获取参考的正确方法是使用django.contrib.auth.get_user_model()
。 e.g:
from django.contrib.auth import get_user_model
User = get_user_model()
class MyModel(models.Model):
user = models.ForeignKey(User)
在Python中,它的约定是小写名称,除非它们是类。
答案 3 :(得分:0)
你应该遵循python约定关于如何使用大写,小写,否则解释器将引用错误的变量:
class User
,class CaseTitle
)。 case_number, user, assigned_user, etc...
当您撰写User = ForeignKey(User)
时,您实际上正在重新定义User
变量,因此在下一行AssignedUser = ForeignKey(User)
中,User
不是User
模型上课了。如果我把它写出来,我会得到:
AssignedUser = models.ForeignKey( # User
models.ForeignKey( # User
models.ForeignKey( # User etc...
...