Django Rest Framework IntegrityError when tried use unique_together -


this question seems similar aiming error

integrityerror column username_id not unique 

what looking having these fields unique when together. meaning same roomid , username can seen in database once when together.
however, user can seen roomid. point avoid multiple submissions of same user , same roomid.
the error above occurs when try submit requst same username roomid.

here model

class active(models.model):     username = models.foreignkey(profile)     roomid = models.foreignkey(room)     activeid = models.uuidfield(primary_key=true, default=uuid.uuid4,                             editable=false)     class meta:         unique_together=(('username', 'roomid',)) 

i using sqlite and.

i thought unique_together constraint did doesn't seem it. what's proper approach?

edit

i ended overriding perform_create , verify no row contains both keys before saving in link.

def perform_create(self, serializer):     queryset = active.objects.filter(username=serializer.data.get('username')).filter(roomid=serializer.data.get('roomid'))     if queryset.exists():         raise validationerror({"detail" : "dont silly"})     serializer.save() 

seems working fine.

regards

sorry, answered: django unique (with foreign keys)

what looking might provided many many relation. can use relation on user or room instead ?


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -