Skip to content
Advertisement

Tag: django

Unsupported lookup ‘category’ for CharField or join on the field not permitted

models.py: This is my views.py file. I show error at: bestseller = Product.objects.filter(product__category__icontains=’BestSeller’) views.py: I want to Bestseller category product at index.html I use Django 4 and pyhton 3.9 Answer You can’t use CharField as a ForeignKey. It should be like this: Or like this (the same logic): This will output all Products in category with name BestSeller.

Nested Serializer save post foreign key in django

In my project, the relationship between Product and Group is ManytoOne. When I tried to post a new product, it cannot work. I’m sure there are more issues with this code and I will appreciate a detailed answer, because I am new to Django and Python. Thank you in advance. models.py serializers.py views.py Answer First change serializers.py: Then change views.py:

VSCode not using test database for Django tests

I’ve got an issue where VSCode’s test feature uses the production database instead of creating a test database. tests.py I have previously created 1 instance of SomeModel, so if you do SomeModel.objects.all() in the shell, it returns a queryset with that one instance. If I run this test from vscode’s tester it will fail. And when I debug it, I

Edit Form Submit handler for a Model

I have a fairly simple django model in a Wagtail CMS; essentially: I need to perform an action when this model is saved via the Wagtail model edit form (eg /admin/section/thingy/123/edit). Currently, I have registered a post_save signal, however this has resulted in the method being called when the model is saved programmatically (via an import sync task). I’ve had

Django join tables with ForeignKey

I’m trying to join 3 tables with ForeignKey but it returns Null values. I’m using select related and also I tried Insight.objects.all() but both are not working. Here are my models: My View: Answer I solved my problem by the below, I could update the DB with the result of the below query

serializer not being called AttributeError: ‘str’ object has no attribute ‘_meta’

I modified the Auth User by using AbstractUser Class. Registered it in settings. Everything else is working I can create an instance using the User Model BUT The Problem comes during the serialization. My serializer doesn’t throw any error, but any time I try and use the serializer.data I get AttributeError: ‘str’ object has no attribute ‘_meta’. User Model Custom

Advertisement