1

New to programming and this is very confusing for me... I have 2 models. Department and Crossfunctionalproject. The relationship between the two models creates a join table called Department_Crossfunctionalproject.

I have another model called employee. I want to add employees to the Department_Crossfunctionalproject table. How can I do this?

I've been following this tutorial: https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships. Can I not do this on the automatically created department_crossfunctionalproject join table that DJANGO creates?

I keep getting the following error:

CommandError("One or more models did not validate:\n%s" % error_text) 
django.core.management.base.CommandError: One or more models did not validate:
companydatabase.membership:"Department_crossfunctionalproject" has a relationship with model <class 'companydatabase.models.Department_crossfunctionalproject'>, 
which has either not been installed or is abstract.

MODELS:

class Crossfunctionalproject(models.Model):
    nameofproject = models.CharField(max_length=50)
    def __unicode__(self):
        return self.nameofproject

class Department(models.Model):
    name = models.CharField(max_length=100)
    project = models.ManyToManyField(Crossfunctionalproject, null=True, blank=True)

class Employee(models.Model):
    employeenumber = models.CharField(max_length=50)
    fname = models.CharField(max_length=50, verbose_name='First Name')
    mname = models.CharField(max_length=50, null=True, blank=True, verbose_name='Middle Name')
    lname = models.CharField(max_length=50, verbose_name='Last Name')
    email = models.EmailField(null=True, blank=True, verbose_name='Email')
    phone = models.CharField(max_length=50, null=True, blank=True, verbose_name='Phone')
    title = models.CharField(max_length=50, null=True, blank=True, verbose_name='Position')

# db table created by DJANGO, I wrote out the model, trying to add extra columns*
class Department_crossfunctionalproject(models.Model):
    department = models.ForeignKey(Department)
    crossfunctionalproject = models.ForeignKey(Crossfunctonalproject)
    membership = models.ManyToManyField(Employee, through="Membership")

class Membership(models.Model):
    employee = models.ForeignKey(Employee)
    department_crossfunctionalproject = models.ForeignKey(Department_crossfunctionalproject)    
    whytheyjoined = models.CharField(max_length=100)
thedeepfield
  • 6,138
  • 25
  • 72
  • 107

4 Answers4

0

I like to manage changes to my models using South: http://south.aeracode.org/

Django doesn't handle dropping columns so I started using it for that.

jabs
  • 1,694
  • 4
  • 19
  • 36
0

I got similar error message, but the cause of my problem was different and hence the fix was different too. My problem was that files that contained definitions of each class did not "import" the other class.

I got the following error:

Unhandled exception in thread started by bound method Command.inner_run of django.contrib.staticfiles.management.commands.runserver.Command object at 0x010E2170

File "C:\ ...\lib\site-packages\django\core\management\base.py", line 284, in validate

raise CommandError("One or more models did not validate:\n%s" % error_text)

django.core.management.base.CommandError: One or more models did not validate: app_name.studio: 'instructors' has an m2m relation with model Instructor, which has either not been installed or is abstract.

I have a ManyToMany relationship defined between Studio and Instructor classes. Each class is defined in a separate file in models directory (not in the same models.py file)

In app_name/models/studio.py :

class Studio(models.Model):
    instructors = models.ManyToManyField("Instructor", blank=True, null=True, db_table="app_name_instructor_studios")

In app_name/models/instructor.py :

class Instructor(models.Model):
    studios = models.ManyToManyField("Studio", blank=True, null=True, db_table="app_name_instructor_studios")

SOLUTION:

I added missing import statement to app_name/models/__init.py__ file as described in this post

from instructor import *

Another way that I was able to solve it is to explicitly import Instructor class in app_name/models/studio.py file (However I prefer the firsts solution):

from app_name.models.instructor import Instructor
Community
  • 1
  • 1
Max Zalota
  • 56
  • 5
0

The problem is that you define Department_Crossfunctionalproject before 'Employee'. Try with providing the name of Employee model as a string:

membership = models.ManyToManyField('Employee', through="Membership")
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
  • i moved the employee model above department_crossfunctionalproject as well as tried employee as a string but still I cant get runserver to work.. still getting the same error command in shell... – thedeepfield Mar 08 '12 at 17:30
  • dont forget to regenerate your database. If you aren't using something like south, then you will have to delete the tables in this app and `python manage.py syncdb` – Francis Yaconiello Mar 08 '12 at 17:52
  • i try to regenerate the database but when i run python manage.py syncdb I get an errors saying: Error: One or more models did not validate: companydb.membership: 'department_crossfunctionalproject' has a relation with model , which has either not been installed or is abstract. – thedeepfield Mar 08 '12 at 18:17
0

Syncdb command creates the database tables for all apps in INSTALLED_APPS whose tables have not already been created. It will never issue ALTER TABLE statements to match changes made to a model class after installation. If you changed the Department_Crossfunctionalproject model by hand you have to drop the table and run syncdb again.

syncdb in django manual

Martin
  • 2,135
  • 8
  • 39
  • 42
  • so I didnt run the python manage.py syncdb command to create the tables, I did it manually. But I dropped the department_crossfunctionalproject to try it syncdb, along with the membership table and I get an error saying: One or more models did not validate: companydb.membership: 'department_crossfunctionalproject' has a relation with model , which has either not been installed or is abstract. – thedeepfield Mar 08 '12 at 17:35
  • Some more typos. Department_Crossfunctionalproject is missing colon on class declaration line. Department_Crossfunctionalproject member in class membership can not be in upper case. – Martin Mar 08 '12 at 17:39
  • thanks, corrected those. Those were errors created when copying the code. – thedeepfield Mar 08 '12 at 17:50
  • Department_Crossfunctionalproject `crossfunctionalproject = models.ForeignKey(Crossfunctonalproject)` also from copying? – Martin Mar 08 '12 at 17:51
  • When i tried syncdb i got different error related to field name clashes. – Martin Mar 08 '12 at 17:59
  • lol yes sorry... they were made when copying over, I had to mask variables. – thedeepfield Mar 08 '12 at 18:03