I need to check if a user exists in the database given their input: either their email or phone number. If the user exists, the user is used to take their order. If the user inputs both their email and phone and they don't exist in the database, I need to create the user. How do I do this?
This is the serializer:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
extra_kwargs = {
'email': {'required': True},
'phone': {'required': True}
}
def create(self, validated_data):
user= User.objects.create(
email=validated_data['email'],
phone=validated_data['phone']
)
user.save()
return user
class OrderSerializer(serializers.ModelSerializer):
food= FoodSerializer(many=True)
class Meta:
model = Order
fields = ('date','food','phone','email')
def validate(self, data):
email = data.get('email')
phone= data.get('phone')
if not email and not phone:
raise serializers.ValidationError("Email or phone required")
return data
def checkuser(self, cliente, data):
if data['email'] or data['phone'] in user:
return user
elif data['email'] or data['phone'] not in user:
pass
def create(self, validated_data):
order = Order.objects.create(
email=validated_data['email'],
phone=validated_data['phone'],
date= validated_data['date'],
food= validated_data['food'],
quantity= validated_data['quantity'],
)
order.save()
return order