2014-04-21 5 views
0

Je voudrais savoir si mes relations de modèle sont correctes. Je suis en train de créer une plate-forme de découverte pour permettre aux utilisateurs de rechercher des produits différents. Merci pour l'aide!Problème avec les relations dans Django Modèle

J'ai quatre models: Produit, Boutique, ProductCategory & Designer

Every Product, has a Designer.

Every Product, has a ProductCategory.

Every Boutique, has a Product and the Product is related to a Designer

Every Designer, has a Product and the Product is related to a Boutique.

Mon model.py code ci-dessous:

Designer

class Designer(models.Model): 
    name = models.CharField(max_length=254, blank=True, null=True) 
    label_name = models.CharField(max_length=254, blank=True, null=True) 
    description = models.CharField(max_length=254, null=True, blank=True) 
    specialites = models.CharField(max_length=254, null=True, blank=True) 
    image = models.ImageField(upload_to='images/designers/main',max_length=100, null=True) #For the argument upload_to, will add to the static folder and generated image will be stored in suing that path specified 

    #For Admin Purposes, to track and see which if still active by for administrative users only 
    is_active = models.BooleanField(default=True) 

    #Foreign Keys & other relationships 
    product = models.ManyToManyField(Product) 
    boutiques = models.ManyToManyField(Boutique) 

    #Metadata 
    class Meta: 
     verbose_name = _("Designer Information") 

    #Helps return something meaningful, to show within the admin interface for easy interaction 
    def __unicode__(self): 
    return self.name 

Produit

class Product(models.Model): 
    name = models.CharField(max_length=254, blank=True, null=True) 
    description = models.CharField(max_length=254, blank=True, null=True)  
    color_name = models.CharField(max_length=254, null=True, blank=True) 
    size_types = models.CharField(max_length=7, null=True, blank=True) 
    product_price = models.DecimalField(max_digits=9,decimal_places=2) 
    old_price = models.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00) #To show original price if, new price has been added 
    product_tags = models.CharField(max_length=254, null=True, blank=True, help_text='Comma-delimited set of SEO keywords for product tag area') 
    novelty = models.CharField(max_length=254, null=True, blank=True) 
    product_website = models.URLField(max_length=200, null=True, blank=True) #To show other sites to Users, where they can purchase the particular product 
    image = models.ImageField(upload_to='images/products/main',max_length=100, null=True) #For the argument upload_to, will add to the static folder and generated image will be stored in suing that path specified 
    slug = models.SlugField(max_length=255, unique=True, help_text='Unique value for product page URL, created from name.') 

    #This shows when each item was uploaded & by who, to the User 
    uploaded_by = models.CharField(max_length=254, blank=True, null=True) 
    uploaded_at = models.DateTimeField(auto_now=True) 

    #For Admin Purposes, to track and see which if still active by for administrative users only 
    is_active = models.BooleanField(default=True) 

    #Foreign Keys & other relationships 
    categories = models.ManyToManyField(ProductCategory) 
    boutiques = models.ManyToManyField(Boutique) 
    designer = models.ForeignKey(Designer) 

    #Metadata 
    class Meta: 
     verbose_name = _("Product") 
     verbose_name_plural = _("Products") 

    #Helps return something meaningful, to show within the admin interface for easy interaction 
    def __unicode__(self): 
     return self.name 

ProductCategory

class ProductCategory(models.Model): 
    name = models.CharField(max_length=255L, blank=True, null=True) 
    slug = models.SlugField(max_length=50, unique=True, help_text='Unique value for product page URL, created from name.') 

    #For Admin Purposes, to track and see which if still active by for administrative users only 
    is_active = models.BooleanField(default=True) 

    #For Admin Purposes, to track when we add each product and each product was updated by administrative users 
    created_at = models.DateTimeField(auto_now_add=True) 
    updated_at = models.DateTimeField(auto_now=True) 

    #Helps return something meaningful, to show within the admin interface for easy interaction 
    def __unicode__(self): 
     return self.name 

Boutique

class Boutique(models.Model): 
     boutique_name = models.CharField(max_length=254, blank=True, null=True) 
     address = models.CharField(max_length=255, blank=True, null=True)  
     city = models.CharField(max_length=50, null=True, blank=True) 
     state = models.CharField(max_length=2, null=True, blank=True) 
     zipcode = models.IntergerField(max_length=5, null=True, blank=True) 
     boutique_website = models.URLField(max_length=200, null=True, blank=True) 

     #For Admin Purposes, to track a product to see which is active by administrative users 
     is_active = models.BooleanField(default=True) 

     #Foreign Keys & other relationships 
     product = models.ManyToManyField(Product) 
     designer = models.ManyToManyField(Designer) 

     #Metadata 
     class Meta: 
      verbose_name = _("Boutique Information") 

     #Helps return something meaningful, to show within the admin interface for easy interaction 
     def __unicode__(self): 
      return self.name 

Répondre

2

Je doute votre models sont correcte.

Je pense que c'est ce que vous essayez de faire:

  1. Il y a un Designer.
  2. Un Designer peut avoir plusieurs Boutiques. Un Boutique peut avoir beaucoup de Products en elle. Un Product est également associé à un ProductCategory.

Ainsi, la relation devrait être comme Designer > Boutique > Productdire A Designer crée un Boutique et ajoute quelques Products dans après l'associer à un ProductCategory.

Cela signifie:

  1. Le Designer ne devrait avoir aucun ForeignKey champs, plutôt il devrait être ForeignKey au Boutique et Product modèles.
  2. Le modèle Boutique devrait avoir un ForeignKey relatif à Designer, pas-Product. Le modèle ProductCategory ne doit pas avoir de champ ForeignKey. Mais il devrait être un ForeignKey à Product.
  3. Le modèle Product devrait avoir troisForeignKey s relatives à Designer, Boutique et ProductCategory.

Ainsi, en tout cela, vos modèles devraient regarder un peu comme ci-dessous:

class Designer(models.Model): 
    name = models.CharField(max_length=100) 
    # Other fields here ... 
    # Don't create any ForeignKey fields 


class Boutique(models.Model): 
    designer = models.ForeignKey(Designer) 
    # Other fields here ... 
    # No need for any more ForeignKeys 


class ProductCategory(models.model): 
    name = models.CharField(max_length=100) 
    # Other fields ... 
    # This model doesn't need any ForeignKey field 


class Product(models.Model): 
    designer = models.ForeignKey(Designer) 
    boutique = models.ForeignKey(Boutique) 
    category = models.ForeignKey(ProductCategory) 
    name = models.CharField(max_length=100) 
    # Other fields here ... 

Il est tout semblable au sondage tutoriel dans docs Django. Là, vous créez un sondage qui a beaucoup de choix. Ainsi, le modèle Choice a un modèle ForeignKey à Poll, pas et vice versa.

Questions connexes