开发者

How do you implement multiple data types for an object in Django?

开发者 https://www.devze.com 2023-02-22 05:39 出处:网络
I\'d like to know the best way to associate various data types with an object in Django.Some of the types should be s开发者_开发技巧tring, boolean, image file, choice from a list, or a link.For exampl

I'd like to know the best way to associate various data types with an object in Django. Some of the types should be s开发者_开发技巧tring, boolean, image file, choice from a list, or a link. For example, say you have a Product model. For product X, you'll want to add an image attribute, a string for the model name, and a link. For product Y, possible attributes would be an image, a weight decimal. What would be the best way to set this up? Are there any packages available that do this or something similar?


You can either make a single model that allows for blank/null values for each field. Or use django's model inheritance if you are setting up types of products that share similar desired attributes or fall into categories. It seems like you are asking for optional attributes which would just require you to define optional fields in the model (first example).

null ref, blank ref

Without Inheritance:

class Product(models.Model):
    name = models.CharField()
    image = models.ImageField(blank=True, null=True)
    weight = models.DecimalField(blank=True, null=True)

Inheritance ref:

class Product(models.Model):
    name = models.CharField()

    class Meta:
        abstract = True

class ProductTypeA(Product):
    image = models.ImageField()

class ProductTypeB(Product):
     weight = models.DecimalField()

edit:

read about relationships in the docs and docs

class Product(models.Model):
    name = models.CharField()

class ProductImage(models.Model):
    product = models.ForeignKey(Product)
    image = models.ImageField()

class ProductWeight(models.Model):
    product = models.ForeignKey(Product)
    weight = models.DecimalField()

class ProductURL(models.Model):
    product = models.ForeignKey(Product)
    url = models.URLField()

class ProductFile(models.Model):
    product = models.ForeignKey(Product)
    file = models.FileField()

The ProductXXXXX are related to the Product model by a foreign key. It is a one to many relationships, so for each product you can have many productxxxx.

As an example, you can make a product:

product_one = Product(name="product_one")
product_one.save()

Now you want to add a weight to that product:

weight = ProductWeight(product=product_one, weight=3.55)
weight.save()

To see all of the weights related to the product:

product_one_weights = product_one.weight_set.all()
for weight in product_one_weights:
    print weight

This allows you to have products with different "attributes".

                      product_one
                           |
       -----------------------------------------
       |             |            |            |
 ProductWeight ProductImage ProductImage ProductFile

                      product_two
                           |
                     --------------
                     |            |            
                ProductURL   ProductImage 


RDBMS with type based structures are not designed for that. For instance, take Google's big table, which don't complain what you store (i.e, product A property types may be entirely different from product B, though both are of type Product).

You need object based storage system, with type flexibility.

Are you sure you want that at any cost? We still can do that, but with lot of overhead. Here is pseudo code.

Model Product:
        id (primary key)

Model AttributeType:
   """
   Defines various data types available for storage.
   ex: (1, 'INT') or (2,'STRING') or (3, 'FLOAT')
   """
      id:
      name:

Model ProductAttributes:
   """
   Stores various attributes of a product
   Ex: (P1,ATT1,100) or (P1, ATT3, 10.5) or (P2, ATT2, "ABCD")
   """
      FK-> Product
      FK-> AttributeType
      Name 'String'
      Value Blob
0

精彩评论

暂无评论...
验证码 换一张
取 消