without herd rancher temporary livestocks in pos - complete
This commit is contained in:
@@ -5,12 +5,14 @@ from apps.authentication.api.v1.serializers.serializer import (
|
||||
CitySerializer
|
||||
)
|
||||
from apps.herd.exception import UniqueRancherApiException
|
||||
from apps.livestock import models as livestock_models
|
||||
from rest_framework import serializers
|
||||
from apps.herd.models import Herd, Rancher
|
||||
|
||||
from django.db.transaction import atomic
|
||||
|
||||
class HerdSerializer(serializers.ModelSerializer):
|
||||
""" Herd Serializer """
|
||||
|
||||
class Meta:
|
||||
model = Herd
|
||||
fields = '__all__'
|
||||
@@ -36,6 +38,26 @@ class RancherSerializer(serializers.ModelSerializer):
|
||||
model = Rancher
|
||||
fields = '__all__'
|
||||
|
||||
def create(self, validated_data):
|
||||
""" create rancher in pos device (without herd) """
|
||||
data = self.context['request'].data.copy()
|
||||
|
||||
with atomic():
|
||||
rancher = Rancher.objects.create(**validated_data) # create rancher
|
||||
if 'livestock' in data.keys():
|
||||
live_stocks = data['livestock']
|
||||
print(live_stocks)
|
||||
livestock_types = {stock.id: stock for stock in livestock_models.LiveStockType.objects.all()}
|
||||
|
||||
for stock in live_stocks:
|
||||
livestock_models.TemporaryLiveStock.objects.create(
|
||||
livestock_type=livestock_types.get(stock.pop('livestock_type')),
|
||||
rancher=rancher,
|
||||
**stock
|
||||
)
|
||||
|
||||
return rancher
|
||||
|
||||
def validate(self, attrs):
|
||||
""" some validations for operations relate to rancher """
|
||||
|
||||
|
||||
Reference in New Issue
Block a user