Creating a shopping List in Django Admin - django

I am trying to show the shopping Cart for only a specific user in the Django Admin template . I managed to do it in a normal views like this :
order = Order.objects.get(user=self.request.user, ordered=False)
but when trying to do the same inside the model
items = models.ManyToManyField(OrderItem.objects.get(user=settings.AUTH_USER_MODEL))
I get this error
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
What I'm trying to achieve is to only show the items that the user entered to his shopping cart instead of all the order items in the system so is there a way to filter what is shown in the admin panel based on the user

Related

View detailed view of model item directly in django admin

Django shows list of model items in admin panel, but don't want to show the list but I directly want to show the detail view . How can i do this ?

Add a custom column in django database

Iam little new to Django and Iam struggling to add column in Django Database.
I was working to create a subcategory for products, but the I want those subcategories to be predefined. That is for example "Fashion" category could have subcategory of 'Men', 'Women', 'Kids'.
I want a table in DB of these subcategories, without having the user or admin panel option to manipulate these field. User can only select article belong to which subcategory.
I go through a few documentation, but could understood much:
1. https://docs.djangoproject.com/en/3.0/howto/custom-model-fields/
2. Adding fields to an already existing database for Django (version < 1.7)
Please suggest me how to add these predefined table values.
Create subcategories model and model admin then in model admin you can disable change permission like this
Subcategories admin model
def has_change_permission(self, request, obj=None):
return False
Now you can add,delete and view only permission. User and admin can't change them.

Django admin for categories and objects

I have objects and catetories in Django and object has ForeignKey to Category.
In Admin user must first create categories and then create objects choosing category from list.
What I want to have is file manager metaphore: User opens list of categories, clicks "create object here" and creates one like we do it with folders and files.
I wonder if Django has some application for it. Does it?
I know about inlines, but I do not want to edit object on the same page, I want to have list of them.
It is similar to Django: adding an "Add new" button for a ForeignKey in a ModelForm but I do not have ModelForm, I speak about admin
In Django admin, you can use inlines, example:
class OBJECTSInline(admin.TabularInline):
model = OBJECTS
extra = 0
class CATEGORYAdmin(admin.ModelAdmin):
list_per_page = 10
inlines = [OBJECTSInline,]
admin.site.register(CATEGORY, CATEGORYAdmin)
If you register both your models in the Django admin, you will see that when creating/editing an object with foreign key to Category, there will be a dropdown for the foreign key value, which lists existing values, and next to it, buttons for editing/deleting the selected value, or for creating a new Category:
That seems to be what you're asking for - it's already there in the admin as soon as you register the relevant models.

Django admin custom queryset from UI

Is it possible with django to add a custom html select to an admin page in order to modify the queryset of a specific model?
For example, I have a "Product" model and inside the Products admin page I get all the product.
I would like to have an html select in that page so that the admin can change the queryset not to show ALL the products but just part of it
Thanks

Order items alphabetically in Django admin site's multi-select box

My team's having a lot of trouble finding items in Django admin site's multi-select drop-downs. How do I order items in multi-select drop-downs alphabetically?
Try doing that with the form that you are entering on Form Admin
from django import forms
class YourForm(forms.ModelForm):
attribute = forms.MultipleChoiceField(queryset=Model.objects.order_by('name'))
class Meta:
model = YourModel