10

When I try to migrate my code I get this error. Here are my code and classes:

from django.db import models
from core.models import Event

class TicketType(models.Model):
    name = models.CharField(max_length=45)
    price = models.DecimalField(max_length=2, decimal_places=2, max_digits=2)
    type = models.CharField(max_length=45)
    amount = models.IntegerField()
    event = models.ForeignKey(Event)

class Meta:
    app_label = "core"


import datetime
from django.core.serializers import json
from django.db import models
from core.models import User


class Event(models.Model):
    page_attribute = models.TextField()
    name = models.TextField(max_length=128 , default="New Event")
    description = models.TextField(default="")
    type = models.TextField(max_length=16)
    age_limit = models.IntegerField(default=0)
    end_date = models.DateTimeField(default=datetime.datetime.now())
    start_date = models.DateTimeField(default=datetime.datetime.now())
    is_active = models.BooleanField(default=False)
    user = models.ForeignKey(User)
    ticket_type=models.ForeignKey('core.models.ticket_type.TicketType')

    class Meta:
            app_label = "core"

Here is the error I get:

CommandError: One or more models did not validate: core.event: 'ticket_type' has a relation with model core.models.ticket_type.TicketType, which has either not been installed or is abstract.

gabi
  • 1,324
  • 4
  • 22
  • 47
  • Your `Event` model appears incomplete; the `ticket_type` field ends in a unterminated string. Where is the rest of that line? – Martijn Pieters Jan 10 '14 at 12:54
  • Are these in two separate files? If so, why? – Daniel Roseman Jan 10 '14 at 12:55
  • Sorry, I added the rest of that line – gabi Jan 10 '14 at 12:58
  • 1
    Yes they are in separate files , I tried to break it up to prevent bigger model.py – gabi Jan 10 '14 at 13:01
  • 1
    Breaking up `models.py` in different files is not a straightforward process, and it's usually a better option to create different apps instead. See this answer: http://stackoverflow.com/a/1160607/276451 Anyway, a `models.py` with a couple hundreds lines is not too big imo. – José Tomás Tocino Jan 10 '14 at 13:07

4 Answers4

35

You're unnecessarily confusing yourself by having these in separate files within the same app.

But your issue is caused by the way you're referenced the target model. You don't use the full module path to the model: you just use 'app_name.ModelName'. So in your case it should be:

ticket_type=models.ForeignKey('core.TicketType')
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 4
    One good reason that I dislike Django. Its lack of flexibility. Are Django developers familiar with the concept of class per file? – Mihai H Aug 25 '14 at 04:12
  • 24
    @MihaiH no of course not, because that is a Java concept which does not apply in Python. And what does that have to do with flexibility? – Daniel Roseman Aug 25 '14 at 07:29
  • 1
    I got the same error message when trying to use `models.OneToOneField('ModelName')`. That works as long as both models are in the same app. Your answer helped me realize that I just needed to include the app name to make it work. – kasperd Nov 12 '15 at 17:20
  • @MihaiH are you aware you're calling "lack of flexibility" what is "lack of constraint"? Python doesn't prevent you from writing models in different files, just you must know how to do that. If you knew Python more you'd love it rather than dislike it. – Alessandro Dentella Dec 18 '18 at 10:27
  • @Alessandro Dentella That was quite some time ago, almost 5 years I would say. My remark was not about Python, it was about Django. I am working with Python among other things for quite some time and I still dislike Django :) – Mihai H Dec 19 '18 at 13:49
8

Another issue can be when using multiple models in separate files missing statement like:

class Meta:
    app_label = 'core_backend'
andilabs
  • 22,159
  • 14
  • 114
  • 151
3

You can also get this error if there a bug in your models file that prevents it from loading properly. For example, in models.py

from third_party_module_i_havent_installed import some_method
Mark Chackerian
  • 21,866
  • 6
  • 108
  • 99
3

I hit this error when I didn't put a third-party app in my INSTALLED_APPS setting yet.

yndolok
  • 5,197
  • 2
  • 42
  • 46