一个表中的多个外键

时间:2022-11-16 11:29:23

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.

我有一个table1,col_a,col_b,col_c,col_c。我有另一个table2,有tb_col1,tb_col2,tb_col3,我想引用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)

我应该使用复合键,如果是这样我如何在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

在这里我如何将price指向product_models并委托给product_models

1 个解决方案

#1


0  

Why not just related sales_process to a product_models like so:

为什么不将product_process与product_models相关联,如下所示:

product_model = models.ForeignKey('product_models')

product_model = models.ForeignKey('product_models')

No need to duplicate multiple columns from product_models on the sales_process model is there?

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

Also, take a look at custom Model Managers: https://docs.djangoproject.com/en/dev/topics/db/managers/

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

You can create convenience methods on a model (to say look up things from a related model) in a nicely named way.

您可以以一种精美的方式在模型上创建便捷方法(以便从相关模型中查找内容)。

In the example below you can call Team.objects.get(pk=1).games() returns all games for a team, home or away.

在下面的示例中,您可以调用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")  

#1


0  

Why not just related sales_process to a product_models like so:

为什么不将product_process与product_models相关联,如下所示:

product_model = models.ForeignKey('product_models')

product_model = models.ForeignKey('product_models')

No need to duplicate multiple columns from product_models on the sales_process model is there?

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

Also, take a look at custom Model Managers: https://docs.djangoproject.com/en/dev/topics/db/managers/

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

You can create convenience methods on a model (to say look up things from a related model) in a nicely named way.

您可以以一种精美的方式在模型上创建便捷方法(以便从相关模型中查找内容)。

In the example below you can call Team.objects.get(pk=1).games() returns all games for a team, home or away.

在下面的示例中,您可以调用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")