-3

I have a table1 , with col_a, col_b, col_c,col_c. I have another table2, with tb_col1, tb_col2, tb_col3, i want to reference col_a ->tb_col, col_b ->tb_col2 , col_c -> tb_col3.

should I use composite keys, If so how do i implement this in Django(python)

my Models :

class product_models(models.Model):
     products = models.ForeignKey('products')
     model_name = models.CharField(max_length=50)
     model_price = models.IntegerField(max_length=4)
     model_desc = models.TextField(blank=True)
     commision = models.IntegerField(max_length=3)

    def __unicode__(self):
        return self.model_name



class sales_process(models.Model):

     prospect = models.ForeignKey('prospect')
     employee = models.ForeignKey(User)
     first_call = models.DateTimeField
     product = models.ForeignKey('products')
     model_name = models.ForeignKey('product_models')
     #price = reference to product_model for the price
     #commission = reference to product_model for commission 

Here how can i refer price to product_models and commission to product_models

4

1 回答 1

0

为什么不像这样将 sales_process 与 product_models 相关联:

product_model = models.ForeignKey('product_models')

不需要在 sales_process 模型上复制 product_models 的多个列吗?

另外,看看自定义模型管理器: https ://docs.djangoproject.com/en/dev/topics/db/managers/

您可以以命名良好的方式在模型上创建便利方法(例如从相关模型中查找事物)。

在下面的示例中,您可以调用 Team.objects.get(pk=1).games() 返回球队的所有比赛,无论是主场还是客场。

class TeamManager(models.Manager):
    def games(self, name):
        return Game.objects.filter(home__name=name) | Game.objects.filter(away__name=name)


class Team(models.Model):
    objects = TeamManager()
    name = models.CharField(max_length=100, unique=True)

class Game(models.Model):
    away = models.ForeignKey(Team, null=True, blank=True, related_name="away")
    home = models.ForeignKey(Team, null=True, blank=True, related_name="home")  
于 2013-10-21T19:46:46.383 回答