Home > Net >  django rest_framework error MultipleObjectsReturned get method return more than one
django rest_framework error MultipleObjectsReturned get method return more than one

Time:11-23

I making a project where everytime someone create an Item, in the ClearingOffice it will increment office_serial 1,

but it's saying get() returned more than one ClearingOffice -- it returned 14! so when I try filter it increment all office_serials

models.py

class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
userid = models.CharField(null=True, max_length=9)
office = models.ForeignKey('ClearingOffice', models.DO_NOTHING, blank=True, null=True)

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []

objects = CustomUserManager()

def __str__(self):
    return self.email

class ClearanceItem(models.Model):
cl_itemid = models.CharField(primary_key=True, max_length=20, default=get_default_id)
studid = models.CharField(max_length=9, blank=True, null=True)
office = models.ForeignKey('ClearingOffice', models.DO_NOTHING, blank=True, null=True)

    class Meta:
    managed = False
    db_table = 'clearance_item'

class ClearingOffice(models.Model):
# this should be office_id
office = models.OneToOneField('Office', models.DO_NOTHING, primary_key=True)
staff = models.TextField(blank=True, null=True)
office_serial = models.IntegerField(blank=True, null=True)

class Meta:
    managed = False
    db_table = 'clearing_office'

signals.py

@receiver(post_save, sender=ClearanceItem)
def create_transaction_log(sender, instance, created, **kwargs):
if created:
  TransactionLog.objects.create(cl_itemid=ClearanceItem.objects.get(cl_itemid=instance.cl_itemid),
     trans_desc="Add Clearance Item",
     trans_recorded=str(datetime.now().strftime('%Y-%m-%d')))
  ClearingOffice.objects.get(office_serial=instance.office.office_serial).update(office_serial=F('office_serial')   1)

the strange part is I have two users one of them is working currectly incrementing his office_serial while the other increments all of them

enter image description here enter image description here

can anyone explain why this is happening?

Edit: I added the serializer.py

class ClearanceItemSerialize(serializers.ModelSerializer):
class Meta:
    model = ClearanceItem
    fields = '__all__'

def create(self, validated_data):
    validated_data["office"] = self.context["request"].user.office
    validated_data["recorded_by"] = self.context["request"].user.userid
    validated_data["cl_itemid"] = self.context["request"].user.office.office_id   validated_data.get('sy')   validated_data.get('sem')   '-'   str(self.context["request"].user.office.office_serial)
    return super().create(validated_data)

CodePudding user response:

You may use id, it will do the same purpose as you desire. It gets assigned to all the users created, and gets incremented by 1 by each user created. In your case the office_serial is getting called for all the instances created according to the request and will be updated accordingly.

  • Related