Sorry about the title, I couldn't find the right word. I'll try my best to describe it properly.
不好意思,我找不到合适的词。我尽量把它描述得恰当些。
I have a business model which needs some properties. But the properties of the business depends on what category it is.
我有一个需要一些属性的商业模型。但企业的性质取决于它属于哪一类。
So for example, I have a business named "XYZ" under the category "Restaurant" and a business named "ABC" under the category "Spa". I need "XYZ" to have a specific set of properties; while "ABC" has a different set of properties. Also, I need to be able to combine categories, so I just can't create a different model for each category.
例如,我在“餐厅”类别下有一家名为“XYZ”的公司,在“Spa”类别下有一家名为“ABC”的公司。我需要“XYZ”具有一组特定的属性;而“ABC”有一组不同的属性。另外,我需要能够组合类别,所以我不能为每个类别创建不同的模型。
I'm thinking of having a "list" in category, which "activates" the appropriate fields the business. Is that possible in Django? Or should I rethink everything?
我在考虑在类别中有一个“列表”,它“激活”业务的适当领域。这在Django是可能的吗?还是我应该重新考虑一切?
3 个解决方案
#1
2
Django normally (in most deployments) maps your models into tables in a relational DB, making your desired architecture really hard to achieve. However, there's a project called django-expando which offers "A reusable Django app allowing model attributes to be assigned dynamically similar to App Engine's built-in expando class.". I don't know how well it works (its readme does mention some limitations, such as the fact that all fields are "treated as string-like" because it's "not storing types" -- so it's a bit more limited than App Engine's built-in Expando
models), but something like it would seem to be the only way to achieve your desired architecture.
Django通常(在大多数部署中)将模型映射到关系数据库中的表中,这使得您想要的体系结构很难实现。然而,有一个名为Django -expando的项目提供了一个可重用的Django应用程序,可以动态地分配模型属性,类似于app Engine的内置expando类。我不知道它是如何工作(其自述提到了一些限制,比如所有字段“视为弦”,因为它的“不存储类型”——这是一个更比App Engine的内置Expando有限模型),但像这似乎是唯一的方法来达到你想要的架构。
#2
0
Couldn't you just create all the erroneous properties that aren't common between all businesses as nullable fields? Then, whatever properties don't apply to any specific kind of business are null. Possible categories of businesses would then just be different combinations of properties depending on what they need.
难道你不能创建所有业务之间不常见的错误属性作为可空字段吗?然后,任何不适用于任何特定类型业务的属性都为null。那么,可能的业务类别就只是不同的属性组合,这取决于它们需要什么。
I'm probably not fully understanding your problem, so feel free to correct me. I also can't claim to be a seasoned django developer, so disregard all of this if it sounds silly to you.
我可能没有完全理解你的问题,所以请随时纠正我。我也不能说自己是一个经验丰富的django开发人员,所以如果你觉得这听起来很傻的话,不要理会这些。
#3
0
Two options spring to mind. This is one I used for a business directory, using the one2one relationship I can list the whole directory in one go or just the business entries. Here the is a shortened version of the model
我想到了两个选择。这是我用于业务目录的一个,使用one2one关系,我可以一次列出整个目录,或者只列出业务条目。这里是模型的缩短版本
class Category(models.Model):
name = models.CharField(max_length=12, unique=True)
description = models.TextField()
class Subcategory(models.Model):
category = models.ForeignKey(Category)
name = models.CharField(max_length=30, unique=True)
class Directory(models.Model):
name = models.CharField(max_length=60)
phone = models.CharField(max_length=15, blank=True)
mobile = models.CharField(max_length=15, blank=True)
etc.
class Business(Directory):
directory = models.OneToOneField(Directory, parent_link=True, related_name="business_entries")
cat = models.ForeignKey(Subcategory, limit_choices_to = {'category__exact': 2})
more fields....
def save(self):
self.category='business'
super(Business, self).save()
def subcatname(self):
return self.subcategory__name
def full_category(self):
return 'Business - '+self.subcategory__name
class Community(Directory):
directory = models.OneToOneField(Directory, parent_link=True, related_name="community_entries")
cat = models.ForeignKey(Subcategory, limit_choices_to = {'category__exact': 3})
class Tourism(Directory):
directory = models.OneToOneField(Directory, parent_link=True, related_name="tourism_entries")
cat = models.ForeignKey(Subcategory, limit_choices_to = {'category__exact': 4})
alternatively, you could pickle your custom data and put it into a text field. This would not be searchable however.
或者,您可以将自定义数据pickle到文本字段中。然而,这是无法搜索的。
#1
2
Django normally (in most deployments) maps your models into tables in a relational DB, making your desired architecture really hard to achieve. However, there's a project called django-expando which offers "A reusable Django app allowing model attributes to be assigned dynamically similar to App Engine's built-in expando class.". I don't know how well it works (its readme does mention some limitations, such as the fact that all fields are "treated as string-like" because it's "not storing types" -- so it's a bit more limited than App Engine's built-in Expando
models), but something like it would seem to be the only way to achieve your desired architecture.
Django通常(在大多数部署中)将模型映射到关系数据库中的表中,这使得您想要的体系结构很难实现。然而,有一个名为Django -expando的项目提供了一个可重用的Django应用程序,可以动态地分配模型属性,类似于app Engine的内置expando类。我不知道它是如何工作(其自述提到了一些限制,比如所有字段“视为弦”,因为它的“不存储类型”——这是一个更比App Engine的内置Expando有限模型),但像这似乎是唯一的方法来达到你想要的架构。
#2
0
Couldn't you just create all the erroneous properties that aren't common between all businesses as nullable fields? Then, whatever properties don't apply to any specific kind of business are null. Possible categories of businesses would then just be different combinations of properties depending on what they need.
难道你不能创建所有业务之间不常见的错误属性作为可空字段吗?然后,任何不适用于任何特定类型业务的属性都为null。那么,可能的业务类别就只是不同的属性组合,这取决于它们需要什么。
I'm probably not fully understanding your problem, so feel free to correct me. I also can't claim to be a seasoned django developer, so disregard all of this if it sounds silly to you.
我可能没有完全理解你的问题,所以请随时纠正我。我也不能说自己是一个经验丰富的django开发人员,所以如果你觉得这听起来很傻的话,不要理会这些。
#3
0
Two options spring to mind. This is one I used for a business directory, using the one2one relationship I can list the whole directory in one go or just the business entries. Here the is a shortened version of the model
我想到了两个选择。这是我用于业务目录的一个,使用one2one关系,我可以一次列出整个目录,或者只列出业务条目。这里是模型的缩短版本
class Category(models.Model):
name = models.CharField(max_length=12, unique=True)
description = models.TextField()
class Subcategory(models.Model):
category = models.ForeignKey(Category)
name = models.CharField(max_length=30, unique=True)
class Directory(models.Model):
name = models.CharField(max_length=60)
phone = models.CharField(max_length=15, blank=True)
mobile = models.CharField(max_length=15, blank=True)
etc.
class Business(Directory):
directory = models.OneToOneField(Directory, parent_link=True, related_name="business_entries")
cat = models.ForeignKey(Subcategory, limit_choices_to = {'category__exact': 2})
more fields....
def save(self):
self.category='business'
super(Business, self).save()
def subcatname(self):
return self.subcategory__name
def full_category(self):
return 'Business - '+self.subcategory__name
class Community(Directory):
directory = models.OneToOneField(Directory, parent_link=True, related_name="community_entries")
cat = models.ForeignKey(Subcategory, limit_choices_to = {'category__exact': 3})
class Tourism(Directory):
directory = models.OneToOneField(Directory, parent_link=True, related_name="tourism_entries")
cat = models.ForeignKey(Subcategory, limit_choices_to = {'category__exact': 4})
alternatively, you could pickle your custom data and put it into a text field. This would not be searchable however.
或者,您可以将自定义数据pickle到文本字段中。然而,这是无法搜索的。