【问题标题】:mapping many-to-one in django and playframework在 django 和 playframework 中进行多对一映射
【发布时间】:2011-09-15 06:25:41
【问题描述】:

我需要为 django 和 Play! 中的应用程序建模客户和地址。我相信两个客户可以有相同的地址。

客户和地址之间的多对一关系

class Customer extends play.db.jpa.Model{

@ManyToOne
public Address address;

..
}

在 django 中,下面的这个 python 代码是否给出了类似的映射?

class Address(models.Model):
   customer= models.ForeignKey(Customer)

创建的表会是什么样子?我在这里有点困惑..

【问题讨论】:

    标签: java django playframework many-to-one


    【解决方案1】:

    你几乎是对的。 many-to-one relationship in Django 确实由models.ForeignKey 表示。

    要表达两个客户可以拥有相同地址的关系,您可以在 Customer 模型中定义该关系(而不是您假设的 Address 模型中)。

    class Customer(models.Model):
        address = models.ForeignKey(Address)
    

    【讨论】:

      猜你喜欢
      • 2017-08-05
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 2013-11-07
      相关资源
      最近更新 更多