Django 1.11无法识别外键模型分配

时间:2017-07-29 09:55:12

标签: python mysql django

我的django模型中有2个外键分配问题。请参阅下面的 models.py

from django.db import models
from django.contrib.auth.models import User


class userData(models.Model):
    user            = models.ForeignKey(User)
    house           = models.CharField(max_length=100)
    address         = models.CharField(max_length=100, blank=True, null=True)
    street          = models.CharField(max_length=150)
    state           = models.CharField(max_length=100)
    postcode        = models.CharField(max_length=20)
    country         = models.CharField(max_length=100)
    telephone       = models.CharField(max_length=20)
    subscription    = models.IntegerField(default=0)
    active          = models.IntegerField(default=0)



class area(models.Model):
    area_name       = models.CharField(max_length=100)
    longitude       = models.CharField(max_length=100)
    latitude        = models.CharField(max_length=100)

class country(models.Model):
    area_name       = models.CharField(max_length=100)
    longitude       = models.CharField(max_length=100)
    latitude        = models.CharField(max_length=100)

class city(models.Model):
    area_name       = models.CharField(max_length=100)
    longitude       = models.CharField(max_length=100)
    latitude        = models.CharField(max_length=100)

class foodType(models.Model):
    food_type_name  = models.CharField(max_length=100)

class restaurant(models.Model):
    restaurant_name = models.CharField(max_length=100)
    food_type       = models.ForeignKey(foodType)
    area            = models.ForeignKey(area)
    country         = models.ForeignKey(country)
    city            = models.ForeignKey(city)
    date_added      = models.DateField()
    main_image      = models.ImageField(blank=True, null=True)
    website         = models.URLField(blank=True, null=True)
    summary         = models.TextField(blank=True, null=True)
    description     = models.TextField(blank=True, null=True)
    featured        = models.IntegerField(default=0)


class restaurantFeature(models.Model):
    feature_name    = models.CharField(max_length=100)
    restaurant_id   = models.ForeignKey(restaurant)

Django Foreign Key not working correctly

图像显示国家和城市,没有正确显示,就像FoodType和Area一样。这些用于添加的按钮显示,mysql数据库显示字段旁边的键。我还尝试重命名国家和城市添加位置后,认为它可能是这些名称。

感谢对此的任何帮助。

1 个答案:

答案 0 :(得分:1)

您遇到此问题是因为您需要引用admin.py中的所有模型。 Django管理员不知道你引用了什么。