Jasurbek Nabijonov
Jasurbek Nabijonov

Reputation: 1745

Django model CharField choice clashes with itself

I was extending my Survey model with models.CharField(choices) but I cannot due to

Error: surveys.Survey.status: (models.E006) The field 'status' clashes with the field 'status' from model 'surveys.survey'.

My Model Code:

class Survey(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    start_date = models.DateTimeField(auto_now_add=True)
    finish_date = models.DateTimeField(blank=True, null=True)
    uuid = models.UUIDField(unique=True, editable=False, db_index=True, default=uuid.uuid4)

    Finished = 'FI'
    OnProgress = 'OP'
    ReportGenerated = 'RG'
    STATUSES = [
        (Finished, 'Finished'),
        (OnProgress, 'OnProgress'),
        (ReportGenerated, 'ReportGenerated')
    ]
    status = response = models.CharField(
        max_length=2,
        choices=STATUSES,
        default=OnProgress,
    )

Other models there I used Survey in case it may help:

class SurveyCategory(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    survey = models.ForeignKey(Survey, on_delete=models.CASCADE)

class SurveyQuestion(models.Model):
    survey = models.ForeignKey(Survey, on_delete=models.CASCADE)
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    response_explanation = models.TextField(blank=True, null=True)

    Not_Responded = 'NR'
    Fully_Implemented = 'FI'
    Partially_Implemented = 'PI'
    Not_Implemented = 'NI'
    SURVEY_RESPONSE_CHOICE = [
        (Not_Responded, 'Not Responded'),
        (Fully_Implemented, 'Fully Implemented'),
        (Partially_Implemented, 'Partially Implemented'),
        (Not_Implemented, ' Not Implemented'),
    ]
    response = models.CharField(
        max_length=2,
        choices=SURVEY_RESPONSE_CHOICE,
        default=Not_Responded,
    )

Upvotes: 0

Views: 113

Answers (2)

Masud Dipu
Masud Dipu

Reputation: 301

You cannot use status = response = models.CharField(). Try use only status = models.CharField(). If you want the same value for response to store on database, then you can do that overriding save() method. for example:

status = models.CharField(
    max_length=2,
    choices=STATUSES,
    default=OnProgress,
)
response = models.CharField(
    max_length=2,
    choices=STATUSES,
    default=OnProgress,
    blank=True,
    null=True
)

def save(self, *args, **kwargs):
    self.response = self.status
    super(Survey, self).save(*args, **kwargs) 

Upvotes: 1

Abdul Aziz Barkat
Abdul Aziz Barkat

Reputation: 21787

You have written:

status = response = models.CharField(
    max_length=2,
    choices=STATUSES,
    default=OnProgress,
)

Note the status = response = here, this is causing issues. Keep it either as:

status = models.CharField(
    max_length=2,
    choices=STATUSES,
    default=OnProgress,
)

Or:

response = models.CharField(
    max_length=2,
    choices=STATUSES,
    default=OnProgress,
)

Upvotes: 1

Related Questions