在我的项目中,我有两个应用程序,beacon_info 和报价,我需要将商店用作我的 beacon_info 应用程序到报价的外键
In my project i have two apps , beacon_info and offers, i need to use the store as a foreign key from my beacon_info app to offers
Beacon_info 应用模型
下面显示的代码是我想在另一个应用程序中将其作为外键的字段,如下所述。
from django.db import models
from django.utils.encoding import smart_unicode
from django.utils import timezone
from datetime import datetime
class store(models.Model):
store_name = models.CharField('Store Name',max_length=100)
store_id = models.CharField('Store_ID', max_length=20, unique=True)
store_email = models.EmailField('Email_ID', max_length=254)
address = models.CharField('Address', max_length=250)
country = models.CharField('Country', max_length=50)
state = models.CharField('State', max_length=50)
city = models.CharField('City',max_length=50)
pincode = models.CharField('Pincode', max_length=15)
contact_no = models.CharField('Contact', max_length=15)
createdat = models.DateTimeField(auto_now=True)
def __unicode__(self):
return smart_unicode(self.id)
提供应用的模型
from django.db import models
from django.utils.encoding import smart_unicode
from beacon_info.models import *
from django.utils import timezone
from datetime import datetime
class offer(models.Model):
offer_code = models.CharField(max_length=50)
store_code = models.ForeignKey('beacon_info.models.store')
entry_exit_type = models.CharField(max_length=50, null=True)
offername = models.CharField(max_length=100, null=True)
description = models.TextField()
membership = models.CharField(max_length=5, default=1)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
createdat =models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return smart_unicode(self.offername)
models.ForeignKey('beacon_info.models.store')
应该是
models.ForeignKey('beacon_info.store')
惯例是使用app_name.model_name
,不需要包含models
。
https://docs.djangoproject.com/en/1.7/ref/models/fields/#django.db.models.ForeignKey
To refer to models defined in another application, you can explicitly
specify a model with the full application label. For example, if the
Manufacturer
model above is defined in another application called
production
, you’d need to use:
class Car(models.Model):
manufacturer = models.ForeignKey('production.Manufacturer')
Beacon_info 应用模型 下面显示的代码是我想在另一个应用程序中将其作为外键的字段,如下所述。
from django.db import models
from django.utils.encoding import smart_unicode
from django.utils import timezone
from datetime import datetime
class store(models.Model):
store_name = models.CharField('Store Name',max_length=100)
store_id = models.CharField('Store_ID', max_length=20, unique=True)
store_email = models.EmailField('Email_ID', max_length=254)
address = models.CharField('Address', max_length=250)
country = models.CharField('Country', max_length=50)
state = models.CharField('State', max_length=50)
city = models.CharField('City',max_length=50)
pincode = models.CharField('Pincode', max_length=15)
contact_no = models.CharField('Contact', max_length=15)
createdat = models.DateTimeField(auto_now=True)
def __unicode__(self):
return smart_unicode(self.id)
提供应用的模型
from django.db import models
from django.utils.encoding import smart_unicode
from beacon_info.models import *
from django.utils import timezone
from datetime import datetime
class offer(models.Model):
offer_code = models.CharField(max_length=50)
store_code = models.ForeignKey('beacon_info.models.store')
entry_exit_type = models.CharField(max_length=50, null=True)
offername = models.CharField(max_length=100, null=True)
description = models.TextField()
membership = models.CharField(max_length=5, default=1)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
createdat =models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return smart_unicode(self.offername)
models.ForeignKey('beacon_info.models.store')
应该是
models.ForeignKey('beacon_info.store')
惯例是使用app_name.model_name
,不需要包含models
。
https://docs.djangoproject.com/en/1.7/ref/models/fields/#django.db.models.ForeignKey
To refer to models defined in another application, you can explicitly specify a model with the full application label. For example, if the
Manufacturer
model above is defined in another application calledproduction
, you’d need to use:class Car(models.Model): manufacturer = models.ForeignKey('production.Manufacturer')