I need to retrieve IDs from multiple queries and add them into a list. Then I tried This raised error as list cannot be added to queryset. so, I tried, This works but, all_products has a mix of int and tuple values [10, 20, 30, (2,), (2,), (1,)] I need them to be [10, 20, 30, 2, 2, 1] Answer
Tag: django
Django execute previous action on refresh
I have tried to add a book in to the database using an HTML form. After the submission, the page redirect to a page where all the books are listed .Then whenever I refresh the page , the data is became duplicated. How do I resolve this problem? urls.py views.py Template file: Answer This is most common proble…
AttributeError: ‘tuple’ object has no attribute ‘model’. How to fix this error?
I’m new to Django. Please help me to solve this error. I’m not getting any solution. I tried many stackoverflow solutions and GitHub solutions but I’m not getting where the error is coming from. urls.py models.py views.py serializers.py Exception log Here is the github link of the app –…
Can’t create superuser in Django using custom user
I got this error when i try to run manage.py createsuperuser TypeError: UserManager.create_superuser() missing 1 required positional argument: ‘username’ My User model: and in my settings.py file: How can i solve this? Im new in Django, documentation is confused to me… I already tried remove…
Django View, foreign key field is killing performance
MyObjects definition: View definition: I do some calculations in my view that are irrelevant and my view takes around 3 seconds for 5000 objects if my_dict[str(my_obj.foreign_key_field)] += 1 is commented out. However, when that line uncommented, my view takes 20seconds. It must be because this field is a for…
How to add a new “comment” or “flag” field to every model field of existing model?
Disclaimer: I can wipe out the database anytime. So while answering this, please don’t care about migrations and stuff. Imagine me having a model with multiple values: Now I want to add a comment to be stored for every value of that model. For example I want to add a comment “measured in winter…
Reducing number of repeated queries in Django when creating new objects in a loop
I’m populating a database using Django from an Excel sheet. In the example below, the purchases list simulates the format of that sheet. My problem is that I’m repeatedly using Customer.objects.get to retrieve the same Customer object when creating new Purchase entries. This results in repeated da…
Trying to add a field to Django many-to-many relation
Right now I’m working on a Django project responsible for the warehouse management system. I got products, customers, warehouse branches, product types, orders, etc… products can be [Milk 1L bottle, cheese 500g pack, ….] The problem I have been facing is that I’m trying to make a many-…
I would like to limit the borrowing objects in my Django app
I have an app that displays games and users can borrow them. I want to limit the amount of borrowed games for each user to max 3 games. I created two models borrow and games. The methods work well except for the max borrowing. Answer You can check whether he has borrowed more than 3 games in form.is_valid() i…
how to select more options in “ManyToManyField()”?
i wanted to create a tag option for a a music model , well mainly musics accept more than one tag and there are amny kind of genres and tags ,so i wanted to make a ManyToManyField() that you can set more than 1 value ,also if you tag is not in add it in. to do that i thought