def update(self, instance, validated_data): item = Item.objects.get(pk=instance.id) item.code = validated_data.pop('code') item.name = validated_data.pop('name') item.description = validated_data.pop('description') item.category = validated_data.pop('category') item.type = validated_data.pop('type') item.unit = validated_data.pop('unit') account_no = validated_data.pop('account').get('account_no') site_id = DEFAULT_PROJECT_ID if item.account: site_id = item.account.site.id if account_no: if item.account: account = item.account if InventoryAccount.objects.filter(account_no=account_no).exists(): account_no = int(account_no) +1 account.account_no = account_no account.save() else: account = InventoryAccount(name=item.name, account_no=account_no, site_id=site_id) account.save() item.account = account item.save() return item
def create(self, validated_data): account_no = validated_data.pop('account').get('account_no') # site_id = validated_data.pop('site').get('id') item = Item.objects.create(**validated_data) # if not site_id: # site_id = DEFAULT_PROJECT_ID site_id = DEFAULT_PROJECT_ID if account_no: if item.account: account = item.account if InventoryAccount.objects.filter(account_no=account_no).exists(): account_no = int(account_no) +1 account.account_no = account_no account.save() else: account = InventoryAccount(name=item.name, account_no=account_no, site_id=site_id) account.save() item.account = account item.save() return item