I have already asked this question but I got no response can you please elaborate how should I proceed. I am trying to create the order but i stuck at logic.
models.py:
class OrderItem(models.Model):
image_number = models.CharField(max_length=20)
title = models.CharField(max_length=20)
image_size = models.CharField(max_length=50)
file_type = models.CharField(max_length=20)
price = models.CharField(max_length=50)
def __str__(self):
return self.title
class Order(models.Model):
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
items = models.ManyToManyField(OrderItem)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
def __str__(self):
return str(self.user)
serializers.py:
class AddtocartSerializers(serializers.ModelSerializer):
class Meta:
model = OrderItem
fields = ['image_number','title','image_size','file_type','price']
class CartSerializers(serializers.ModelSerializer):
class Meta:
model = Order
fields = ['item',
'start_date',
'ordered_date'
]
views.py:
class AddtocartView(generics.CreateAPIView):
authentication_classes = []
permission_classes = []
pagination_class = None
queryset=OrderItem.objects.all()
serializer_class = AddtocartSerializers
class CartView(generics.ListCreateAPIView):
authentication_classes = []
permission_classes = []
pagination_class = None
queryset=Order.objects.all()
serializer_class = CartSerializers
def create(self, request):
return super(AddtocartView, self).create(request, *args, **kwargs)
urls.py:
path('addtocart/',views.AddtocartView.as_view(),name='addtocart'),
path('cart/',views.CartView.as_view(),name='cart'),
how can i create order after addtocart?
Yes you can do a post request to addtocart endpoint. But there must be logic written in addtocart endpoint to create an order and add OrderItem to the order. Then the order number must be returned to browser so that user can resume shopping / checkout the order.
After receiving the order number you can do some javascript stuff to show user his order details.
First of my suggestion for you don't to use ModelViewSet, because all you need is create and update for you view, for create use CreateAPIView, for update RetrieveAPIView.
you code will be like below:
class OrderItemSerializer(serializers.ModelSerializer)
class Meta:
model = OrderItem
fields = ['id', 'image_number', 'image_size', 'file_type', 'price']
class OrderCreateSerializer(serializers.ModelSerializer)
user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
items = serializers.ListField(child=serializers.PrimaryKeyRelatedField(queryset=OrderItem.objects.all(), required=True), required=True)
ordered_date = serializers.DateField()
class Meta:
model = Order
fields = ['user', 'items', 'ordered_date']
def create(self, validated_data):
validated_data['user'] = self.context['request'].user
instance = Order.objects.create(**validated_data)
instance.save()
return instance
class CartView(CreateAPIView):
authentication_classes = []
queryset=Order.objects.all()
serializer_class = OrderCreateSerializer
def create(self, request):
return super(AddtocartView, self).create(request, *args, **kwargs)
ModelViewSet include more code which you don't use in you case. And also you can get user from request, and you can just add
validated_data['user'] = self.context['request'].user
Related
I have two models, I have to make an endpoint where the results of two tables should appear in the json, which have a fongeringkey that joins them.
My code is the following:
models.py
class Property(models.Model):
address = models.CharField(max_length=120)
city = models.CharField(max_length=32)
price = models.BigIntegerField()
description = models.TextField(blank=True, null=True)
year = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'property'
class StatusHistory(models.Model):
property = models.ForeignKey(Property, on_delete=models.CASCADE)
status = models.ForeignKey(Status, on_delete=models.CASCADE)
update_date = models.DateTimeField()
class Meta:
managed = False
db_table = 'status_history'
views.py
class StandardResultsSetPagination(PageNumberPagination):
page_size = 10
page_size_query_param = "page_size"
max_page_size = 1000
class PropertyListView(viewsets.ModelViewSet):
http_method_names = ['get', 'head']
serializer_class = PropertyListSerializer
queryset = Property.objects.all()
pagination_class = StandardResultsSetPagination
def get_serializer_class(self):
if self.action == 'list':
return PropertyListSerializer
return PropertyListSerializer
def get_queryset(self):
queryset = Property.objects.all()
if self.request.GET.get('year'):
queryset = queryset.filter(year=self.request.GET.get('year'))
if self.request.GET.get('city'):
queryset = queryset.filter(city=self.request.GET.get('city'))
if self.request.GET.get('status'):
#what would I do here?
pass
else:
queryset = queryset.order_by('-year')
return queryset
def list(self, request):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
serializers.py
class PropertyListSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Property
fields = ('id', 'address', 'city', 'price', 'description', 'year')
class StatusHistoryListSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = StatusHistory
fields = ('property', 'status', 'update_date')
I don't know how to filter by get with the parameters, city, year(model Property) and status(model StatusHistory).
You only need to filter based in the status of the history you can do the following:
if self.request.GET.get('status'):
queryset = queryset.filter(statushistory__status=self.request.GET.get('status'))
I don't know why I have this error, I didn't have it befor, I'm sure that I'm logged in before trying call the api, please find below the code :
views.py
class AlbumCreate(generics.CreateAPIView):
serializer_class = AlbumsSerializer
def perform_create(self, serializer):
owner2 = self.request.user
serializer.save(owner=owner2)
class AlbumList(generics.ListAPIView):
permission_classes = [IsAuthenticated]
queryset = Album.objects.all()
serializer_class = AlbumsSerializer
class AlbumDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Album.objects.all()
serializer_class = AlbumsSerializer
Serializer
class AlbumsSerializer(serializers.ModelSerializer):
owner = serializers.StringRelatedField(read_only=True)
class Meta:
model = Album
fields = "__all__"
models
def upload_path(instance, filname):
return '/'.join(['covers', str(instance.title), filname])
class Album(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=30)
cover = models.IntegerField(default=0)
photos_number = models.IntegerField(default=0)
image = models.ImageField(blank=True, null=True, upload_to=upload_path)
def __str__(self):
return self.title
django rest framework gives me this error when i am trying to perform a post request to create a new order even though i specified the usr value in my serializer.save method.
here is my view
class OrderListCreateAPI(generics.ListCreateAPIView):
serializer_class = OrderSerializer
permission_classes = [IsOwner]
def get_queryset(self):
user = self.request.user
return Order.objects.filter(user= user)
def perform_update(self, serializer):
instance = serializer.save(user = self.request.user)
order = Order.objects.get(id = instance['id'].value)
order.item.quantity -= order.quantity
order.save()
my models.py
payment_methods = ((1,'credit card'),(2, 'cash'),(3, 'paypal'))
class Product(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
image = models.ImageField(upload_to = 'images/', height_field=300, width_field=300)
price = models.IntegerField()
quantity = models.IntegerField()
seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name='products')
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
class Order(models.Model):
item = models.ForeignKey(Product, on_delete= models.CASCADE, related_name='orders')
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='orders')
quantity = models.IntegerField()
payment_options = models.CharField(choices=payment_methods, max_length=50)
Delivery = models.CharField(max_length=200)
my serializers
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ['id', 'title', 'description','image', 'price', 'quantity', 'seller', 'date']
read_only_fields = ['date', 'seller']
class OrderSerializer(serializers.ModelSerializer):
class Meta:
model = Order
fields = '__all__'
read_only_fields = ['user']
You need to use perform_create and not perform_update in your view.
class OrderListCreateAPI(generics.ListCreateAPIView):
serializer_class = OrderSerializer
permission_classes = [IsOwner]
def get_queryset(self):
user = self.request.user
return Order.objects.filter(user= user)
def perform_create(self, serializer):
instance = serializer.save(user = self.request.user)
order = Order.objects.get(id = instance['id'].value)
order.item.quantity -= order.quantity
order.save()
I have a field owner that is a ForeignKey to User model.
This field is required at the time of creation. But it can not be changed later on.
How to make fields Non-Editable? Is there any other way than creating multiple serializers?
Garage Model
class GarageDetails(models.Model):
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, )
name = models.CharField(_('Garage Name'), max_length=254, blank=False, null=False, unique=True)
price = models.IntegerField(_('Price'), blank=False)
available_from = models.TimeField(_('Available From'), default=datetime.time(6, 00), blank=False)
available_till = models.TimeField(_('Available till'), default=datetime.time(18, 00), blank=False)
description = models.TextField(_('Garage Description'), blank=True, null=True)
create_date = cmodels.UnixTimestampField(_('Date Added'), auto_now_add=True)
update_date = cmodels.UnixTimestampField(_('Date Added'), auto_created=True)
is_available = models.BooleanField(_('Available'), default=True)
Serializer
class UserFKSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = ('id', 'name', 'email', 'mobile')
class GarageSerializer(serializers.ModelSerializer):
owner = UserFKSerializer(many=False, read_only=True)
class Meta:
model = GarageDetails
fields = '__all__'
read_only_fields = ('id', 'owner', 'create_date', 'update_date')
Views
class GarageRegister(generics.ListCreateAPIView):
renderer_classes = (JSONRenderer, )
permission_classes = (IsAuthenticated, )
#csrf_exempt
def post(self, request, *args, **kwargs):
serialize = GarageSerializer(data=request.data)
if serialize.is_valid():
# Create Garage with owner & name
class GarageUpdate(generics.ListCreateAPIView):
renderer_classes = (JSONRenderer, )
permission_classes = (IsAuthenticated, )
#csrf_exempt
def post(self, request, *args, **kwargs):
serialize = GarageSerializer(data=request.data)
if serialize.is_valid():
# Update Garage but can't update create_date, id, owner & name
You could create a different model serializer for each use case (update, create):
specifying that field in read_only_fields in your model serializer:
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = ('a', 'nother', 'field')
read_only_fields = ('owner',)
for django forms instead you set the disabled field:
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
form.fields['owner'].widget.attrs['disabled'] = True
You can do this by overriding the "update" method as follows:
def update(self, instance, validated_data):
if 'owner' in validated_data:
del validated_data['owner']
return super().update(instance, validated_data)
This will silently ignore the owner field on updates. If you want to you may instead "raise ValidationError('owner may not be set on updates')" but if you do so you may want to read the model instance and only raise the error if it's actually a change to avoid false positives.
Also, if you're using the python2, the "super" call needs to be "super(GarageSerializer, self)" or some such.
I have an "Event" model and it has a many2many field with default user model.
class Event(models.Model):
name = models.CharField(max_length=50)
type = models.CharField(max_length=50)
location = models.CharField(max_length=255)
start_hour = models.CharField(max_length=50)
end_hour = models.CharField(max_length=50)
creator = models.CharField(max_length=50)
info = models.CharField(max_length=255, default='')
users = models.ManyToManyField(User)
def __str__(self):
return self.name
Now, I am trying to update this many2many field like following;
//my serializers
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id',)
class EventSerializer(serializers.ModelSerializer):
users = UserSerializer(many=True)
class Meta:
model = Event
fields = ('id', 'users')
def update(self, instance, validated_data):
submitted_users = validated_data.get('users')
if submitted_users:
for user in submitted_users:
user_instance = User.objects.get(id=user.id)
instance.users.add(user_instance)
instance.save()
return instance
//views.py
class UpdateParticipants(generics.RetrieveUpdateDestroyAPIView):
queryset = Event.objects.all()
serializer_class = EventSerializer
However, I am getting an error like in the belowe image
// this is the APIView that I used
Can you try with this code below?
Models
Example
class UserModel(User):
pass
class Event(models.Model):
name = models.CharField(max_length=50)
type = models.CharField(max_length=50)
location = models.CharField(max_length=255)
start_hour = models.CharField(max_length=50)
end_hour = models.CharField(max_length=50)
creator = models.CharField(max_length=50)
info = models.CharField(max_length=255, default='')
users = models.ManyToManyField(User, related_name='event_user') # Set related name to User object for Rest Framework
def __str__(self):
return self.name
Serializer
class EventSerializer(serializers.ModelSerializer):
users = serializers.PrimaryKeyRelatedField(queryset=User.objects.all(), required=False)
class Meta:
model = Event
class Userserializer(serializers.ModelSerializer):
event_user = EventSerializer(many=True)
class Meta:
model = UserModel
def update(self, instance, validated_data):
event_user = validated_data.pop('event_user', None)
print (event_user)
if validated_data:
for attr, value in validated_data.items():
if attr == 'password':
instance.set_password(value)
else:
setattr(instance, attr, value)
instance.save()
return instance
Views
class EventView(APIView):
permission_classes = (AllowAny,)
def get(self):
qv = User.objects.all()
serializer = Userserializer(qv, many=True)
return Response(data={'users': serializer.data}, status=status.HTTP_200_OK)
def put(self, request, user_id):
instance = Event.objects.get(user=user_id)
serializer = Userserializer(instance=instance,data=request.data, partial=True)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(data={'users': serializer.data}, status=status.HTTP_200_OK)
return Response(data={'users': serializer.errors}, status=status.HTTP_403_FORBIDDEN)
URL-s
urlpatterns = [
url(r'^users/(?P<user_id>\d+)',EventView.as_view())
]
JOSN exaple for put
JSON
{
"id":1,
"event_user":[{
"name":"Changed Name"
}]
}