2013-03-05 255 views
0

讓我們這些模型:繼承或設置,選項

from .choices import STATUS_CHOISES 

class Status(models.Model): 
    current_status = models.CharField("Current status", max_length=50, choices=STATUS_CHOICES, default='new') 
    status_change_date = models.DateField(verbose_name="Status change date", default=datetime.datetime.now()) 

class ProductRequest(models.Model): 
    destination = models.CharField("Product Destination", max_length=255) 
    status = models.ForeignKey(Status) 

選擇:

STATUS_CHOICES = (
('new', 'New'), 
('ongoing', 'Ongoing'), 
('finalized', 'Finalized'), 
) 

我需要設置相同的默認,並從ForeignKey的原始模型使用相同的選擇。我怎樣才能做到這一點?

+0

看看這篇文章:http://www.mechanicalgirl.com/post/動態的選擇上-A-外鍵字段/。希望它會給你一些想法。 – alecxe 2013-03-05 11:54:47

+0

我認爲這是不可能的,因爲外鍵是基於PK而不是選擇 – catherine 2013-03-05 13:09:26

回答

0

決定要簡單的屬性:

class ProductRequest(models.Model): 
    destination = models.CharField("Product Destination", max_length=255) 
    status = models.CharField("Request Status", max_length=50, choices=STATUS_CHOICES, default='new') 

尋找一種方式來存儲狀態更改日期..