user3292290
user3292290

Reputation: 3

ERROR :- One or more models did not validate

I am new in django. My question is that:- I got an error(below describe) when apply syncdb

ERROR: One or more models did not validate:

my models.py

     class practicalArea:
        practical_area = models.CharField(max_length=25)

        class Meta:
            db_table = 'practical_area'

        def __unicode__(self):
            return self.practical_area


class Profile(models.Model):
        auth_user_id = models.ForeignKey(User, related_name='userProfile')
        address = models.TextField(blank=True)        
        state = models.CharField(max_length=20)
        practical_area = models.ForeignKey('practicalArea', related_name='practical_Area')        
        company = models.CharField(max_length=40)     
        photo = models.CharField(max_length=250)
        created_date = models.DateTimeField()
        modify_date = models.DateTimeField()


        class Meta:
            db_table = 'adminPanal_profile'


        def save(self):
            if self.created_date == None:
                self.created_date = datetime.now()
            self.modify_date = datetime.now()
            super(Profile, self).save()

if i change "practical_area = models.ForeignKey('practicalArea', related_name='practical_Area')" to "practical_area = models.ForeignKey(practicalArea, related_name='practical_Area')"

then I got an ERROR:

AssertionError: ForeignKey() is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'

please help!

Upvotes: 0

Views: 791

Answers (2)

remy
remy

Reputation: 4743

Your practicalArea class should subclass models.Model as well. Try it like this:

class PracticalArea(models.Model):
    practical_area = models.CharField(max_length=25)

    class Meta:
        db_table = 'practical_area'

    def __unicode__(self):
        return self.practical_area


class Profile(models.Model):
    auth_user_id = models.ForeignKey(User, related_name='userProfile')
    address = models.TextField(blank=True)        
    state = models.CharField(max_length=20)
    practical_area = models.ForeignKey('PracticalArea', related_name='practical_Area')        
    company = models.CharField(max_length=40)     
    photo = models.CharField(max_length=250)
    created_date = models.DateTimeField()
    modify_date = models.DateTimeField()


    class Meta:
        db_table = 'adminPanal_profile'


    def save(self):
        if self.created_date == None:
            self.created_date = datetime.now()
        self.modify_date = datetime.now()
        super(Profile, self).save()

Upvotes: 1

CrazyGeek
CrazyGeek

Reputation: 3437

You should give Model Name directly instead of giving it in string because django expect compete path to model class if you give model name in string.

    practical_area = models.ForeignKey(practicalArea, related_name='practical_Area')       

As you are doing it for User.

Upvotes: 0

Related Questions