0

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)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry i dont follow, do you mean in ClearingOffice.objects.get(office_serial=instance.office.office_serial).update(office_serial=F('office_serial') + 1) i should replace get.(office_id=instance.office.office_id) ?
I'm at home now maybe tomorrow i'll try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.