Search code examples
djangomany-to-manymodels

django m2m model not syncing


When I run syncdb on my new m2m model I get the error:

Error: One or more models did not validate: services.service: 'categories' specifies an m2m relation through model Service_Category, which has not been installed

I tried using the example listed on https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships

and when I synced that it worked fine. SO I am not getting something right with my model below. Not really sure why my model isnt working and if its a simple typo or something else. NOTE: The models worked fine using a simple m2m relationship but it doesnt like it this way. Thanks

from django.db import models

# Create your models here.




class Category(models.Model):

    name = models.CharField(max_length=50)
    slug = models.SlugField(max_length=50, unique=True,
                            help_text='Unique value for product page URL, created from name.')
    description = models.TextField()
    is_active = models.BooleanField(default=True)
    meta_keywords = models.CharField("Meta Keywords", max_length=255, blank = True, null = True,
                                     help_text='Content for description meta tag')
    meta_description = models.CharField(max_length = 255, blank = True, null = True,
                                        help_text = 'Content for description meta tag')
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

    class Meta:
        #app_label = ''
        db_table = 'categories'
        ordering = ['created_at']
        verbose_name_plural = 'Categories'

    def __unicode__(self):
        return self.name

    @models.permalink   
    def get_absolute_url(self):
        return ('catalog_category', (), {'category_slug': self.slug})


class Service(models.Model):

    name = models.CharField(max_length=255, unique = True)
    slug = models.SlugField(max_length=255, unique = True,
                            help_text = 'Unique value for product page URL, created from name.')


    old_price = models.DecimalField(max_digits=9, decimal_places=2,
                                    blank = True, null = True, default = 0.0)
    image = models.CharField(max_length=50, blank = True)
    is_active = models.BooleanField(default = True)
    is_bestseller = models.BooleanField(default = False)
    is_featured = models.BooleanField(default = False)
    description = models.TextField()
    bullet1 = models.CharField(max_length=255, blank = True, null = True,
                               help_text = 'Option Bullet for description')
    bullet2 = models.CharField(max_length=255, blank = True, null = True,
                               help_text = 'Option Bullet for description')
    bullet3 = models.CharField(max_length=255, blank = True, null = True,
                               help_text = 'Option Bullet for description')
    bullet4 = models.CharField(max_length=255, blank = True, null = True,
                               help_text = 'Option Bullet for description')
    bullet5 = models.CharField(max_length=255, blank = True, null = True,
                               help_text = 'Option Bullet for description')    
    micro_current = models.BooleanField(default = False)

    meta_keywords = models.CharField(max_length = 255,blank = True, null = True, 
                                     help_text = 'Comma Delimited Set of SEO keywords for meta tag')
    meta_description = models.CharField(max_length = 255, blank = True, null = True,
                                        help_text = 'Content for description meta tag')
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)


    categories = models.ManyToManyField(Category, through='Service_Category')


    class Meta:
        #app_label = ''
        db_table = 'services'
        ordering = ['created_at']


    def __unicode__(self):
        return self.name


class Service_Category:

    category = models.ForeignKey(Category)
    service = models.ForeignKey(Service)
    micro_current = models.BooleanField(default = False)


    class Meta:
        #app_label = ''
        db_table = 'service_categories'

services.service: 'categories' specifies an m2m relation through model Service_Category, which has not been installed


Solution

  • You missed to define your intermediate model correctly

    class Service_Category(models.Model):
    
        category = models.ForeignKey(Category)
        service = models.ForeignKey(Service)
        micro_current = models.BooleanField(default = False)
    
    
        class Meta:
            #app_label = ''
            db_table = 'service_categories'
    

    We have to inherit each of our model which we define from models.Model class. Rest everything seems to be fine.