When I am going to delete product from my cart it gives me error FOREIGN KEY constraint failed
class Cart(TimeStamp):
user = models.ForeignKey('authentication.User', on_delete=models.CASCADE, related_name='user_carts', null=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True, blank=True)
sub_total_price = models.DecimalField(max_digits=100, decimal_places=2, blank=True, null=True)
quantity = models.PositiveIntegerField(default=1)
here is model first I thought the problem would be on_delete and I changed it to SET_NULL but it was useless and it did not work, I tried to delete all files from migrations folder it also did not solve my problem.
here is views.py
class CartUpdateDestroyView(generics.RetrieveUpdateDestroyAPIView):
queryset = Cart.objects.all()
serializer_class = CartSerializer
permission_classes = (IsOwnerOrAdmin,)
lookup_field = 'id'
def get_queryset(self):
return Cart.objects.filter(user=self.request.user)
in this view except Destroy all are working properly but I cannot delete the object. Any idea please?
views.py
def cart_update(request):
product_id = request.POST.get('product_id')
if product_id is not None:
try:
product_obj = Product.objects.get(id=product_id)
except Product.DoesNotExist:
print("Show message to user, product is gone?")
return redirect("cart:home")
cart_obj, new_obj = Cart.objects.new_or_get(request)
if product_obj in cart_obj.products.all():
cart_obj.products.remove(product_obj)
added = False
else:
cart_obj.products.add(product_obj) # cart_obj.products.add(product_id)
added = True
request.session['cart_items'] = cart_obj.products.count()
# return redirect(product_obj.get_absolute_url())
if request.is_ajax(): # Asynchronous JavaScript And XML / JSON
print("Ajax request")
json_data = {
"added": added,
"removed": not added,
"cartItemCount": cart_obj.products.count()
}
return JsonResponse(json_data)
return redirect("cart:home")
models.py
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
products = models.ManyToManyField(Product, blank=True)
subtotal = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
total = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
total1 = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
objects = CartManager()
def __str__(self):
return str(self.id)
Related
i'm trying to filter logbook report based on the logged in industry supervisor,
the supervisor should be able to see the report of students under his supervision
views.py
class LogbookEntryView(ListAPIView):
queryset = LogbookEntry.objects.all()
serializer_class = StudentLogbookEntrySerializer
def get_queryset(self, *args, **kwargs):
qs = super().get_queryset(*args, **kwargs)
request = self.request
user = request.user
if not user.is_authenticated:
LogbookEntry.objects.none()
return qs.filter(student_id__industrysupervisor = request.user.student.industry_based_supervisor)
models.py
LogbookEntry Model
class LogbookEntry(models.Model):
week = models.ForeignKey("api.WeekDates", verbose_name=_("Week Id"), null=True, on_delete=models.SET_NULL)
student = models.ForeignKey("students.Student", verbose_name=_("Student Id"), on_delete=models.CASCADE)
entry_date = models.DateTimeField()
title = models.CharField(_("Title"), max_length=50)
description = models.CharField(_("Description"), max_length=1000)
diagram = models.ImageField(_("Diagram"), upload_to=profile_picture_dir)
Student Model
class Student(models.Model):
user = models.OneToOneField(get_user_model(), null=True, on_delete=models.CASCADE)
profile_pic = models.ImageField(_("profile picture"), upload_to=profile_picture_dir)
department_id = models.ForeignKey(Department, null=True, on_delete=models.SET_NULL)
phone_no = models.CharField(max_length=11)
school_based_supervisor = models.ForeignKey("school_based_supervisor.SchoolSupervisor", verbose_name=_("School Supervisor"), null=True, on_delete=models.SET_NULL)
industry_based_supervisor = models.ForeignKey("industry_based_supervisor.IndustrySupervisor", verbose_name=_("Industry Supervisor"), null=True, on_delete=models.SET_NULL)
placement_location = models.ForeignKey("industry_based_supervisor.PlacementCentre", verbose_name=_("Placement Location"), null=True, blank=True, on_delete=models.SET_NULL)
Industry Supervisor Model
class IndustrySupervisor(models.Model):
user = models.OneToOneField(get_user_model(), null=True, on_delete=models.CASCADE)
profile_pic = models.ImageField(_("profile picture"), upload_to=profile_picture_dir)
phone_no = models.CharField(max_length=11)
placement_center = models.ForeignKey("industry_based_supervisor.PlacementCentre", verbose_name=_("Placement Centre"), null=True, blank=True, on_delete=models.CASCADE)
def __str__(self):
return self.user.username
You can update your get_queryset method in the LogbookEntryView class
def get_queryset(self):
user = self.request.user
# If user is not authenticated, return empty queryset
if not user.is_authenticated:
return LogbookEntry.objects.none()
# If user is not a student, return empty queryset
if not hasattr(user, 'student'):
return LogbookEntry.objects.none()
# Get the industry supervisor for the student
supervisor = user.student.industry_based_supervisor
# If student does not have an industry supervisor, return empty queryset
if supervisor is None:
return LogbookEntry.objects.none()
# Filter logbook entries based on the industry supervisor
queryset = LogbookEntry.objects.filter(student__industrysupervisor=supervisor)
return queryset
I am getting Issue while edit a record based on CHatquestion ID, if option is null then i need to add a record based on same chatquestion id, if chatqustion id exist in option it will work,
i am trying to multiple way to solve this issue but still can't find solution.
Models.py # thease are all 3 models
class Problem(models.Model):
Language = models.IntegerField(choices=Language_CHOICE, default=1)
type = models.CharField(max_length=500, null=True, blank=True)
def __str__(self):
return self.type
class ChatQuestion(models.Model): # Eding record based on chatquestion id
question = RichTextField(null=True, blank=True)
problem_id = models.ForeignKey(
Problem,
models.CASCADE,
verbose_name='Problem',
)
def __str__(self):
return self.question
is_first_question = models.BooleanField(default=False)
class Option(models.Model):
option_type = models.CharField(max_length=250, null=True, blank=True)
question_id = models.ForeignKey(
ChatQuestion,
models.CASCADE,
verbose_name='Question',
null=True,
blank=True
)
problem=models.ForeignKey(
Problem,
models.CASCADE,
verbose_name='Problem',
null=True,
blank=True
)
next_question_id = models.ForeignKey(ChatQuestion, on_delete=models.CASCADE, null=True, blank=True,
related_name='next_question')
def __str__(self):
return self.option_type
forms.py
class EditQuestionForm(forms.ModelForm):
class Meta:
model = ChatQuestion
fields =('question','problem_id')
class EditOptionForm(forms.ModelForm):
class Meta:
model = Option
fields =('option_type',)
views.py
def question_edit(request,id=None):
if id is not None:
queryset = get_object_or_404(ChatQuestion,pk=id)
queryset1=get_object_or_404(Option,question_id=queryset )
else:
queryset = None
queryset1 = None
if request.method=="POST":
form = EditQuestionForm(request.POST ,instance=queryset)
form1=EditOptionForm(request.POST, instance=queryset1)
if form.is_valid() and form1.is_valid():
question=form.cleaned_data['question']
option_type=form1.cleaned_data['option_type']
if id:
queryset.question=question
queryset.save()
queryset1.option_type=option_type
queryset1.save()
messages.success(request,'Sucessful')
return redirect('/fleet/list_chatbot')
else:
print(form.errors)
messages.error(request,'Please correct following',form.errors)
elif id:
form = EditQuestionForm(instance=queryset)
form1=EditOptionForm(instance=queryset1)
if not queryset1:
form1=EditOptionForm()
else:
form = EditQuestionForm()
form1=EditOptionForm()
context={
'form':form,
'form1':form1
}
return render(request,'chatbot/question_edit.html',context=context)
So many documentation for filtering in Django rest framework but all the examples are in class based view. but I am trying to do the same in DRF function based view. I wanted to do multiple filter for my items queryset.
I tried one way and it is working perfectly. Here first I am trying to search by item name or restaurant name in one request. then I take another keyword and try to filter restaurant name or item name based on restaurant city. It is working perfectly like if I hit this url
http://localhost:8000/api/items/?keyword=lasagne&keyword1=paris
then it gives me the perfect response.
But What I am asking for is that now my code looks for this specific part is messy and I want to add more fields for multiple filtering. Which procedure to follow? Should I follow this one and multiple requests and trying to filter from this.
Suppose now I want to filter the queryset based on dish_type, price, item_type, location and then search for items by name or restaurants by name
#this is my models
class Restaurant(models.Model):
user = models.OneToOneField(CustomUser, related_name='restaurant', on_delete=models.CASCADE, null=False)
name = models.CharField(max_length=200, blank=True, null=True)
profile_picture = models.ImageField(null=True, blank=True)
address = models.TextField(max_length=2000, blank=True, null=True)
city = models.CharField(max_length=200)
latitude = models.DecimalField(max_digits = 13, decimal_places = 7, blank=True, null=True)
longitude = models.DecimalField(max_digits = 13, decimal_places = 7, blank=True, null=True)
is_verified = models.BooleanField(default=False)
createdAt = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.name)
class Item(models.Model):
user = models.ForeignKey(Restaurant, on_delete=models.CASCADE)
name = models.CharField(max_length=220)
image = models.ImageField(null=True, blank=True)
dish_type = models.ForeignKey(Dishtype, on_delete=models.CASCADE)
item_type = models.ForeignKey(Itemtype, on_delete=models.CASCADE)
description = models.TextField(max_length=10000)
rating = models.DecimalField(max_digits=7, decimal_places=2, blank=True, null=True)
numReviews = models.IntegerField(null=True, blank=True, default=0)
old_price = models.DecimalField(max_digits=11, decimal_places=2)
discount = models.IntegerField(blank=True, null=True)
price = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
countInStock = models.IntegerField(blank=True, null=True, default=0)
createdAt = models.DateTimeField(auto_now_add=True)
_id = models.AutoField(primary_key=True, editable=False)
def save(self, *args, **kwargs):
self.price = Decimal(self.old_price * (100 - self.discount) / 100)
return super(Item, self).save(*args, **kwargs)
class Meta:
ordering = ['-createdAt']
def __str__(self):
return self.name
#serializer
class RestaurantSerializer(serializers.ModelSerializer):
user = serializers.SerializerMethodField(read_only=True)
class Meta:
model = Restaurant
fields = '__all__'
def get_user(self, obj):
user = obj.user
serializer = UserSerializer(user, many=False)
return serializer.data
class ItemSerializer(serializers.ModelSerializer):
user = serializers.SerializerMethodField(read_only=True)
dish_type = serializers.SerializerMethodField(read_only=True)
item_type = serializers.SerializerMethodField(read_only=True)
class Meta:
model = Item
fields = '__all__'
def get_user(self, obj):
user = obj.user
serializer = RestaurantSerializer(user, many=False)
return serializer.data
def get_dish_type(self, obj):
dish_type = obj.dish_type
serializer = DishtypeSerializer(dish_type, many=False)
return serializer.data
def get_item_type(self, obj):
item_type = obj.item_type
serializer = ItemtypeSerializer(item_type, many=False)
return serializer.data
#views.py
#api_view(['GET'])
#permission_classes([IsAuthenticated])
def getItems(request):
user = request.user
query = request.query_params.get('keyword')
if query == None:
query = ''
cuery = request.query_params.get('keyword1')
if cuery == None:
cuery = ''
items = Item.objects.select_related('user').select_related('dish_type').select_related('item_type').all().filter(
Q(name__icontains = query) | Q(user__name__icontains = query))
else:
restaurant_city = Item.objects.select_related('user').select_related('dish_type').select_related('item_type').all(
).filter(Q(user__city__iexact = cuery))
items = restaurant_city.filter(Q(name__icontains = query) | Q(user__name__icontains = query))
serializer = ItemSerializer(items, many=True)
return Response(serializer.data)
######Updated solved the problem
#filters.py
class ItemFilter(django_filters.FilterSet):
numReviews = django_filters.NumberFilter()
numReviews__gt = django_filters.NumberFilter(field_name='numReviews', lookup_expr='gt')
numReviews__lt = django_filters.NumberFilter(field_name='numReviews', lookup_expr='lt')
name = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Item
fields = ['_id', 'dish_type__id']
#views
#api_view(['GET'])
#permission_classes([IsAuthenticated])
def getItems(request):
user = request.user
queryset = Item.objects.all()
filterset = ItemFilter(request.GET, queryset=queryset)
if filterset.is_valid():
queryset = filterset.qs
serializer = ItemSerializer(queryset, many=True)
return Response(serializer.data)
now data are passing like this
http://localhost:8000/api/items/?numReviews__gt=20&numReviews__lt=22
You can use queryset and override get_queryset function.
class FooViewSet(GenericViewSet, mixins.ListModelMixin):
authentication_classes = [JSONWebTokenAuthentication]
permission_classes = [IsAuthenticated]
serializer_class = ItemSerializer
def get_queryset(self):
query = self.request.query_params.get('keyword', '')
if not self.request.query_params.get('keyword1'):
items = Item.objects.select_related('user').select_related('dish_type').select_related(
'item_type').all().filter(
Q(name__icontains=query) | Q(user__name__icontains=query))
else:
restaurant_city = Item.objects.select_related('user').select_related('dish_type').select_related(
'item_type').all(
).filter(Q(user__city__iexact=self.request.query_params.get('keyword1', '')))
items = restaurant_city.filter(Q(name__icontains=query) | Q(user__name__icontains=query))
return items
I am trying to get a list of orders of only those products associated with that merchant. In my project, every product is associated with a merchant. In the merchant dashboard, merchants should be able to view only their products and orders.
When I try to filter the orders based on products associated with that merchant, I got the above error.
My models:
class Product(models.Model):
merchant = models.ForeignKey(Seller,on_delete=models.CASCADE,blank=True,null=True)
category = models.ManyToManyField(Category, blank=False)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
class Order(models.Model):
ORDER_STATUS = (
('To_Ship', 'To Ship',),
('Shipped', 'Shipped',),
('Delivered', 'Delivered',),
('Cancelled', 'Cancelled',),
)
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
#items = models.ManyToManyField(OrderItem,blank=True, null=True,related_name="order_items")
#start_date = models.DateTimeField(auto_now_add=True)
order_status = models.CharField(max_length=50,choices=ORDER_STATUS,default='To_Ship')
ordered_date = models.DateTimeField(auto_now_add=True)
ordered = models.BooleanField(default=False)
total_price = models.CharField(max_length=50,blank=True,null=True)
#billing_details = models.OneToOneField('BillingDetails',on_delete=models.CASCADE,null=True,blank=True,related_name="order")
def __str__(self):
return self.user.email
class Meta:
verbose_name_plural = "Orders"
ordering = ('-id',)
class OrderItem(models.Model):
#user = models.ForeignKey(User,on_delete=models.CASCADE, blank=True)
order = models.ForeignKey(Order,on_delete=models.CASCADE, blank=True,null=True,related_name='order_items')
item = models.ForeignKey(Product, on_delete=models.CASCADE,blank=True, null=True)
order_variants = models.ForeignKey(Variants,on_delete=models.CASCADE,blank=True,null=True)
quantity = models.IntegerField(default=1)
total_item_price = models.CharField(max_length=50,blank=True,null=True)
def __str__(self):
return f"{self.quantity} items of {self.item} of {self.order.user}"
class Meta:
verbose_name_plural= "Cart Items"
ordering = ('-id',)
My views:
class SellerOrderAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = OrderItemSerializer
def get_queryset(self):
merchant = get_object_or_404(Seller,self.kwargs['pk'])
return OrderItem.objects.all(item__merchant=merchant)
My serializers:
class OrderItemSerializer(serializers.ModelSerializer):
order_variants = VariantSerializer(read_only=True)
#order_variants =VariantSerializer()
item = ProductSerializer(read_only=True)
class Meta:
model = OrderItem
fields = ['id','order','item','order_variants', 'quantity']
# depth = 1
Here, I saw that when I see the orderdetails, merhchant is inside item, that's why I tried filtering the queryset using item__merchant=merchant, but I got this error.
My urls:
path('api/merchantorder/<int:pk>', views.SellerOrderAPIView.as_view(), name='api-merchant-orderdetail'),
You need to specify the name of the field on which you filter:
class SellerOrderAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = OrderItemSerializer
def get_queryset(self):
merchant = get_object_or_404(Seller, pk=self.kwargs['pk'])
return OrderItem.objects.filter(item__merchant=merchant)
you can however simply avoid the lookup and work with:
class SellerOrderAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = OrderItemSerializer
def get_queryset(self):
return OrderItem.objects.filter(item__merchant=self.kwargs['pk'])
I need to filter a model by two values that none of them is the PK of the model.
I want to filter the payments for an specific car and week, this means that I can have more than one payment for a car and in a week.
I'm using Django-Tables2 to display the results.
views.py
class PagosDetailView(SingleTableMixin, DetailView):
template_name = "AC/paymentsbycarandweek.html"
context_table_name = 'table'
model = Pagos
table_class = PagosTable
slug_url_kwarg = 'carro_id'
def get_queryset(self):
qs = super(PagosDetailView, self).get_queryset()
return qs.filter(carro=self.kwargs['carro'], semana=self.kwargs['semana'])
models.py
class Pagos(models.Model):
carro = models.ForeignKey(
Carros, on_delete=models.CASCADE, blank=False, null=False)
pago = models.DecimalField(max_digits=6, decimal_places=2)
fecha = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
semana = models.CharField(max_length=20)
startweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
endweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
renta = models.ForeignKey(
Renta, on_delete=models.PROTECT, blank=False, null=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = "Pagos"
def get_absolute_url(self):
return reverse('pagos')
def __str__(self):
return self.semana
urls.py
path('paymentsbycarandweek/<int:carro>/<slug:semana>',
views.PagosDetailView.as_view(), name='pagos_bycar')
This last time that I have tried several options, I'm getting the following error message:
Generic detail view PagosDetailView must be called with either an object pk or a slug in the URLconf.
How exactly can I pass these two parameters?
After searching a lot, this is how I finally resolved it:
class PagosDetailView(SingleTableMixin, ListView):
model = Pagos
table_class = PagosDetailTable
template_name = 'AC/paymentsbycarandweek.html'
paginate_by = 10
def get(self, request, *args, **kwargs):
self.object_list = self.get_queryset()
self.object_list = self.object_list.filter(
carro=kwargs['carro'], semana=kwargs['semana'])
context = self.get_context_data()
return self.render_to_response(context)