在Django中重新使用一组模型字段 [英] Re-using set of model fields in Django

查看:138
本文介绍了在Django中重新使用一组模型字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站有两种类型的用户;客户和供应商。我结束了以下类结构:

  class Customer(models.Model):
#客户

class供应商(models.Model):
#特定于供应商的东西

class Profile(models.Model):#由Django的User.get_profile )
user = models.ForeignKey(User,unique = True)
customer = models.OneToOneField(Customer,null = True,unique = True)
supplier = models.OneToOneField = True,unique = True)

现在我需要为这些模型添加多个地址:




  • 客户和供应商都应该有他们的用户地址,所以应该在配置文件

  • 客户也有帐单地址应该在客户端,然后



地址将是这样的:

  class Address(models.Model):
street1 = models.CharField(max_length = 100)
street2 = models.Cha rField(max_length = 100)
zipcode = models.CharField(max_length = 16)
city = models.CharField(max_length = 100)
state = models.CharField(max_length = 100)
country = models.CharField(max_length = 100)

我可以看到两个简单的解决方案,使用地址信息:请参阅通过ForeignKey从Profile&客户课程,或继承Profile&



我可以看到两种方法的一些优点:



ForeignKey




  • 将地址数据很好地分配在一个字段后面,而不是污染模型

  • 将来没有多重继承的风险



继承




  • 更容易访问字段

  • 更少的数据库连接

  • 地址在Django的管理员中自动显示对象



你会去哪一个,为什么?你认为任何一种解决方案本身都是坏的吗?你会看到一些更好的选择吗?

解决方案

我可能会用外键方法 - 我觉得这很干净。 >

处理您列表中的优点继承:


  1. 很容易访问字段属于Foreign Keyed的对象 - my_customer.address.zipcode 不是太难,是吗?

  2. 我怀疑你我们会发现加入价格非常昂贵,如果您在获取大量客户/供应商时总是可以避免使用。

  3. 如果您定义了一个Django管理员,您可以随时在Django管理员中显示地址 __ unicode __()方法地址,只需添加地址 (如果这是您所称的ForeignKey属性客户)到您的管理员的 list_display


My site has two types of users; customers and suppliers. I have ended up with the following class structure:

class Customer(models.Model):
    # stuff specific to customers

class Supplier(models.Model):
    # stuff specific to suppliers

class Profile(models.Model):  # returned by Django's User.get_profile()
    user = models.ForeignKey(User, unique=True)
    customer = models.OneToOneField(Customer, null=True, unique=True)
    supplier = models.OneToOneField(Supplier, null=True, unique=True)

Now I need to add multiple addresses to these models:

  • both customers and suppliers should have their "user" address, so should be in Profile
  • customers also have a billing address, should be in Customer then

Address would be something like:

class Address(models.Model):
    street1 = models.CharField(max_length=100)
    street2 = models.CharField(max_length=100)
    zipcode = models.CharField(max_length=16)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100)
    country = models.CharField(max_length=100)

I can see two straightforward solutions to re-use the address information: either refer to Address via ForeignKey from Profile & Customer classes, or inherit Profile & Customer classes from Address.

I can see some pros for both approaches:

ForeignKey

  • groups the address data nicely behind one field instead of "polluting" the model
  • no risk of multiple inheritance in future

Inheritance

  • easier access to fields
  • fewer database joins
  • addresses are automatically visible with the object in Django's admin

Which one would you go with and why? Do you think either of the solutions is inherently bad? Do you see some better alternatives?

解决方案

I would probably go with the Foreign Key approach - I think it's cleaner.

Dealing with the "pros" of inheritance you list:

  1. It's easy to access fields belonging to an object that is Foreign Keyed - my_customer.address.zipcode isn't too hard, is it?
  2. I doubt you'll find joins prohibitively expensive, and if you do you can always avoid them when fetching lots of Customers/Suppliers.
  3. You can always display addresses alongside the object in Django's admin if you define a __unicode__() method on Address, and just add address (if that's what you call the ForeignKey property of Customer) to your admin's list_display.

这篇关于在Django中重新使用一组模型字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆