class Customer(AbstractUser):
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, null=True, blank=True)
birth_year = models.PositiveSmallIntegerField(null=True, blank=True)
class Meta:
ordering = ['last_name', 'first_name']
def __unicode__(self):
return self.username
class CustomerList(generics.ListCreateAPIView):
queryset = Customer.objects.all()
serializer_class = CustomerSerializer
class CustomerSerializer(serializers.ModelSerializer):
class Meta:
model = Customer
fields = ('username', 'password', 'first_name', 'last_name', 'email', 'is_active', 'gender', 'birth_year')
def restore_object(self, attrs, instance=None):
if instance:
customer = instance
customer.username = attrs['username']
customer.first_name = attrs['first_name']
customer.last_name = attrs['last_Name']
customer.email = attrs['email']
if attrs['is_active']:
customer.is_active = attrs['is_active']
if attrs['gender']:
customer.gender = attrs['gender']
if attrs['birth_year']:
customer.birth_year = attrs['birth_year']
else:
customer = Customer(username = attrs['username'],
first_name = attrs['first_name'],
last_name = attrs['last_name'],
email = attrs['email']
)
customer.set_password(attrs['password'])
customer.save()
return customer
IntegrityError at /api/v1/customers/
duplicate key value violates unique constraint "customers_customer_pkey"
DETAIL: Key (id)=(6) already exists.
class CustomerList(generics.ListCreateAPIView):
queryset = Customer.objects.all()
serializer_class = CustomerSerializer
def pre_save(self, obj):
obj.password = make_password(obj.password)