RuntimeError:应用程序中的'userprofile_roles'模型冲突

时间:2017-07-26 12:16:20

标签: python django django-upgrade

我是django的新手,我有一个基于django 1.6.X的项目,我想将它升级到1.11.X以添加一些功能但是当我这样做时我发现了很多问题并且我解决了其中的一些但是这个一个我不知道该怎么做它,当我运行它时它给了我:

RuntimeError: Conflicting 'userprofile_roles' models in application 'survey': <class 'survey.models.UserProfile_roles'> and <class 'survey.models.Userprofile_Roles'>.

完整的error_log:

Unhandled exception in thread started by <function wrapper at 0x7f2b1a21b140>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11.3-py2.7.egg/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11.3-py2.7.egg/django/core/management/commands/runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11.3-py2.7.egg/django/utils/autoreload.py", line 250, in raise_last_exception
    six.reraise(*_exception)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11.3-py2.7.egg/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11.3-py2.7.egg/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11.3-py2.7.egg/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11.3-py2.7.egg/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/media/ahmed-mohamed/My Stuff/Work/AgileEngage.Sample/survey/models.py", line 255, in <module>
    class Userprofile_Roles(models.Model):    
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11.3-py2.7.egg/django/db/models/base.py", line 325, in __new__
    new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
  File "/usr/local/lib/python2.7/dist-packages/Django-1.11.3-py2.7.egg/django/apps/registry.py", line 224, in register_model
    (model_name, app_label, app_models[model_name], model))
RuntimeError: Conflicting 'userprofile_roles' models in application 'survey': <class 'survey.models.UserProfile_roles'> and <class 'survey.models.Userprofile_Roles'>.

任何人都可以帮助我吗?非常感谢。

修改

这是survey / models.py

from django.db import models
from stripogram import html2text
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.core import serializers
from datetime import datetime

class AnswerRange(models.Model):
    title = models.CharField(max_length=200)
    order = models.IntegerField(default=0)

    def answers(self):
        result = ""
        for i in self.mcqanswer_set.all():
            result += "," + i.title
        return result[1:]

    def __unicode__(self):
        return self.title + '( ' + self.answers() + ' )'

    def extar_mcq(self):
        return serializers.serialize('python', self.mcqanswer_set.all())





class McqAnswer(models.Model):
    answerrange = models.ForeignKey(AnswerRange)
    title = models.CharField(max_length=200)
    minValue = models.FloatField()
    maxValue = models.FloatField()
    def __unicode__(self):
        return self.title

class Role(models.Model):
    code = models.CharField(max_length=10)
    title = models.CharField(max_length=200)
    description = models.TextField()
    rank=models.IntegerField(default=1)



    def __unicode__(self):
        return '[ %s ] %s ' % (self.code, self.title)

ANSWER_RANGE_DISPLAY_CHOICES = (
    ('sequence', 'sequence'),
    ('reverse', 'reverse'),
    ('random', 'random'),
)
ACTIVE_CHOICES = (
    ('include', 'No'),
    ('exclude', 'Yes'),
)
class Indicator(models.Model):
    code = models.CharField(max_length=10)
    roles = models.ManyToManyField(Role)
    question = models.TextField()
    answer_range = models.ForeignKey(AnswerRange)
    answer_range_display = models.CharField(max_length=8, choices=ANSWER_RANGE_DISPLAY_CHOICES, default='sequence')
    answers = models.ManyToManyField(User, through='Answer')
    exclude = models.CharField(max_length=8, choices=ACTIVE_CHOICES, default='include')

    def text(self):
        return html2text(self.question)

    def __unicode__(self):
        return '[ %s ] %s ' % (self.code, self.question)

class CharacteristicCategory(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()   
    def __unicode__(self):
        return self.title


class Characteristic(models.Model):
    code = models.CharField(max_length=10)
    title = models.CharField(max_length=200)
    description = models.TextField()
    characteristic_category = models.ForeignKey(CharacteristicCategory, null=True, blank=True)
    indicators = models.ManyToManyField(Indicator)

    def __unicode__(self):
        return '[ %s ] %s ' % (self.code, self.title)


class Practice(models.Model):
    code = models.CharField(max_length=10)
    title = models.CharField(max_length=200)
    description = models.TextField()
    characteristics = models.ManyToManyField(Characteristic)

    def __unicode__(self):
        return '[ %s ] %s ' % (self.code, self.title)


class Answer(models.Model):
    indicator = models.ForeignKey(Indicator)
    user = models.ForeignKey(User)
    mcqanswer = models.ForeignKey(McqAnswer)


class Macro(models.Model):
    place_holder = models.CharField(max_length=255)
    translation = models.TextField()

    def __unicode__(self):
        return '%s -> %s' % (self.place_holder ,self.translation) 

class ReportPassword(models.Model):
    url = models.CharField(max_length=400)
    password = models.CharField(max_length=20)

    def __unicode__(self):
        return self.url  

class Demographic(models.Model):
    title = models.CharField(max_length=200)
    required = models.BooleanField()
    viewable = models.BooleanField()
    def __unicode__(self):
        return self.title

class DemographicValue(models.Model):
    demographic = models.ForeignKey(Demographic)
    value = models.CharField(max_length=200)

    def __unicode__(self):
        return self.value




class UserProfile(models.Model):
    user = models.OneToOneField(User)
    first_name = models.CharField(max_length=80)
    last_name = models.CharField(max_length=80)
    roles = models.ManyToManyField(Role, null=True, blank=True)
    supervisor= models.CharField(max_length=80)
    department_manager= models.CharField(max_length=80)
    demographics = models.ManyToManyField(Demographic, through='UserDemographics')
    hide_welcome_message = models.BooleanField()
    did_fill_demographics = models.BooleanField()
    survey_finished = models.BooleanField()




    def __unicode__(self):
        return self.user.username



class Option(models.Model):    
    key = models.CharField(max_length=80)
    value = models.CharField(max_length=80)
    def __unicode__(self):
        return self.key



def isValidSurveyOption(key,value):
    try:
        option=Option.objects.get(key=key)
    except Exception:
        option = None

    if option is not None:
        if(option.value== value):
            return True

    return False


def addSurveyOptionIfNotExist(key,value):
   try:
        option=Option.objects.get(key=key)
   except Exception:
        option = None
   if option is not None:
        option.value= value
        option.save()
   else:
       Option.objects.create(key=key,value=value)   

def create_user_profile(sender, **kwargs):
    """When creating a new user, make a profile for him or her."""
    u = kwargs["instance"]
    if not UserProfile.objects.filter(user=u):
        UserProfile(user=u).save()

post_save.connect(create_user_profile, sender=User)

class UserDemographics(models.Model):
    userProfile = models.ForeignKey(UserProfile)
    demographic = models.ForeignKey(Demographic)
    demographic_value = models.ForeignKey(DemographicValue)

class Message(models.Model):
    code = models.CharField(max_length=40)
    body = models.TextField(max_length=1500)

    def __unicode__(self):
        return '%s -> %s' % (self.code ,self.body) 

class InstanceSetting(models.Model):
    code = models.CharField(max_length=100)
    value = models.TextField()

    def __unicode__(self):
        return '%s -> %s' % (self.code ,self.value) 

class Comment(models.Model):
    user = models.ForeignKey(User)
    text = models.TextField()

    def __unicode__(self):
        return self.text[:50]


class Test(models.Model):
    code = models.CharField(max_length=40)
    value=models.IntegerField(default=1)


class UsersWaitingList(models.Model):
    first_name = models.CharField(max_length=80)
    last_name = models.CharField(max_length=80)
    email= models.CharField(max_length=80)
    role = models.CharField(max_length=80)
    supervisor= models.CharField(max_length=80)
    department_manager= models.CharField(max_length=80)
    def __unicode__(self):
        return self.email


class ticket(models.Model):
    code=models.TextField()
    type=models.TextField()
    data=models.TextField()
    status=models.TextField()
    date_created = models.DateTimeField()
    date_modified = models.DateTimeField()

    def save(self, *args, **kwargs):
        if self.date_created == None:
            self.date_created = datetime.now()
        self.date_modified = datetime.now()
        super(ticket, self).save(*args, **kwargs) 



class Userprofile_Roles(models.Model):    
    role = models.ForeignKey(Role)
    userprofile = models.ForeignKey(UserProfile)
    def __unicode__(self):
        return self.userprofile.first_name

1 个答案:

答案 0 :(得分:1)

您的多个字段UserProfile.roles与您的模型Userprofile_Roles发生冲突。

class UserProfile(models.Model):
    roles = models.ManyToManyField(Role, null=True, blank=True)

您似乎可以使用through选项表示您要使用Userprofile_Roles

class UserProfile(models.Model):
    roles = models.ManyToManyField(Role, through='Userprofile_Roles', null=True, blank=True)

但是,由于您的Userprofile_Roles未添加任何额外字段,因此保留UserProfile模型可能更为简单,并删除Userprofile_Roles模型。