2013-04-26 68 views
0

我有以下型號:添加多個標籤模型

class Investor(Profile): 
ROLE = (
    ('AN', 'Angel Investor'), 
    ('VC', 'Venture Capital'), 
    ('SC', 'Seed Capital') 
) 
role = models.CharField(max_length=2, default='AN', choices=ROLE) 
min_inv = models.DecimalField(
    default=0, max_digits=20, decimal_places=2, 
    verbose_name='Minimum Investments per year') 
max_inv = models.DecimalField(
    default=0, max_digits=20, decimal_places=2, 
    verbose_name='Maximum investments per year') 
no_startups = models.IntegerField(
    default=0, verbose_name='Number of investments per year') 
rating_sum = models.FloatField(default=0) 
no_raters = models.IntegerField(default=0) 

我想多個標籤添加到這個模型。 - 類別 - 舞臺 - 資金

我希望投資者要分配給多個類別,階段和資金。 因此,投資者可以綁定到多個類別 和多個階段 和多個資金類型。

我該如何編輯模型?

回答

1

你能爲相連模型添加ManyToMany關係麼?

class Investor(Profile): 
    ROLE = (
    ('AN', 'Angel Investor'), 
    ('VC', 'Venture Capital'), 
    ('SC', 'Seed Capital') 
) 
    role = models.CharField(max_length=2, default='AN', choices=ROLE) 
    min_inv = models.DecimalField(
    default=0, max_digits=20, decimal_places=2, 
    verbose_name='Minimum Investments per year') 
    max_inv = models.DecimalField(
    default=0, max_digits=20, decimal_places=2, 
    verbose_name='Maximum investments per year') 
    no_startups = models.IntegerField(
    default=0, verbose_name='Number of investments per year') 
    rating_sum = models.FloatField(default=0) 
    no_raters = models.IntegerField(default=0) 
    categories = models.ManyToMany(Category) 
    stages = models.ManyToMany(Stage) 
    fundings = models.ManyToMany(Funding) 

使用多對多然後你可以分配階段

investor = Investor.objects.all()[0] 
investor.categories.add(category_instance_one, category_instance_two) 
investor.categories.all() # retrieves all categories that this investor has