如何在Django中正确定义直通模型

时间:2016-04-08 12:57:56

标签: python django django-models

当我尝试在Django中定义直通模型时,我遇到了问题。几个小时前我发布了一个问题,答案是定义一个通过课程。但问题是,这个通过模型调用已经定义的模型,并且在这个直通模型之前定义的这些模型之一就是通过模型来调用它。

NameError: name 'UserProfile' is not defined

如果我切换它们:

TranslatorProfile

THIS IS THE ANSWER(实际上它不是UserProfile而是from __future__ import unicode_literals # from django.contrib.auth.base_user import AbstractBaseUser from django.contrib.auth.models import User from django.db import models class AdminContact(models.Model): email = models.EmailField() telephone = models.CharField(max_length=40) def __unicode__(self): return self.email class Language(models.Model): shortcut = models.CharField(max_length=40) name = models.CharField(max_length=40) def __str__(self): return self.name class Level(models.Model): LEVEL_CHOICES = ( ('standard','Standard level'), ('professional','Professional level'), ('native','Native speaker level'), ) name = models.CharField(max_length=40,choices=LEVEL_CHOICES, blank=False, null=False) price_multiplier = models.FloatField() class UserProfile(models.Model): user = models.OneToOneField(User, related_name='userprofile') date_of_birth = models.DateField(null=True,blank=True) telephone = models.CharField(max_length=40,null=True,blank=True) IBAN = models.CharField(max_length=40,null=True,blank=True) created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) MARITAL_STATUS_CHOICES = ( ('single', 'Single'), ('married', 'Married'), ('separated', 'Separated'), ('divorced', 'Divorced'), ('widowed', 'Widowed'), ) marital_status = models.CharField(max_length=40, choices=MARITAL_STATUS_CHOICES, null=True, blank=True) HOW_DO_YOU_KNOW_ABOUT_US_CHOICES = ( ('coincidence', u'It was coincidence'), ('relative_or_friends', 'From my relatives or friends'), ) how_do_you_know_about_us = models.CharField(max_length=40, choices=HOW_DO_YOU_KNOW_ABOUT_US_CHOICES, null=True, blank=True) # TRANSLATOR ATTRIBUTES is_translator = models.BooleanField(default=False) language_tuples = models.ManyToManyField(LanguageTuple,blank=True) languages = models.ManyToManyField(Language, through='TranslatorLanguage') rating = models.IntegerField(default=0) number_of_ratings = models.BigIntegerField(default=0) def __unicode__(self): return '{} {}'.format(self.user.first_name, self.user.last_name) def __str__(self): return '{} {}'.format(self.user.first_name, self.user.last_name) class UserProfileLanguage(models.Model): userprofile = models.ForeignKey(UserProfile) language = models.ForeignKey(Language) level = models.ForeignKey(Level) class Meta: unique_together = (('UserProfile', 'language'),)

你知道该怎么办吗?

我附上models.py以防万一:

Mage_Catalog_Model_Product_Attribute_Backend_Media

2 个答案:

答案 0 :(得分:1)

您应该使用模型字符串而不是模型类本身来引用外键。当你有定义序列问题时,这是特别正确的。引自django doc foreign key section

  

如果您需要在尚未使用的模型上创建关系   定义后,您可以使用模型的名称,而不是模型   对象本身:

from django.db import models

class Car(models.Model):
    manufacturer = models.ForeignKey(
        'Manufacturer',
        on_delete=models.CASCADE,
    )
    # ...

class Manufacturer(models.Model):
    # ...
    pass

答案 1 :(得分:0)

您需要将TranslatorLanguage模型指定为字符串,因为它稍后在models.py中定义,如下所示:

languages = models.ManyToManyField(Language, through='TranslatorLanguage')