Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

django - How to solve non_field_errors error concerning unique_together relationship

Having my class Plug

class Plug(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    station = models.ForeignKey(Station, related_name="plugs", on_delete=models.CASCADE)
    ocpp_name = models.CharField(max_length=50, null=True, blank=True)

I defined a unique_together relationship as below in the Plug model :

class Meta:
    unique_together = ('station', 'ocpp_name',)

The strange thing is that every time I try to create a new plug instance in a specific station given the ocpp_name I got the error attached in the screenshot.

enter image description here

The stack trace raises no error, just a 400 Error response

How can i solve that? Is that related with the serializer of the Plug model?

question from:https://stackoverflow.com/questions/65951940/how-to-solve-non-field-errors-error-concerning-unique-together-relationship

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The solution was more obvious than I had expected.

Due to my db structure I had to change the declaration of unique_together to

unique_together = ('station_id', 'ocpp_name',)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...