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.
Related
I have a User model that has these two fields:
permissions = models.ManyToManyField(Permission)
groups = models.ManyToManyField(Group)
I register the model in the admin. When I view the user model that I made in the admin section I get a multiple select menu that looks like this.
I would much prefer the much better-looking menu like the one that is in the auth user model that comes built into Django admin (looks like this)
Any ideas on what I need to do to be able to access this sort of select menu?
Thanks for your help.
Django newb from PHP (finally)
Use filter_horizontal (or filter_vertical) field in your admin class.
For example:
#admin.register(User)
class UserAdmin(admin.ModelAdmin):
...
filter_horizontal = 'groups', 'permissions', 'any_other_m2m_field'
...
Also link to Django docs:
https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.filter_horizontal
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.
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
I need to create custom users in my app.
In the example given in the doc
class CustomUser(models.Model):
user = models.OneToOneField(User)
#custom fields
a user must exists before creating a CustomUser.
What I want to do is to create automatically a User when I create a CustomUser.
In the CustomUser admin (only visible by the superuser), I'd like to have only the custom fields and a few fields from the User model, as well as some form to allow the superuser to change the password for existing instance.
Anybody could help?
The first part of your question is easy, you can use a signal:
def create_custom_user(sender, instance, created, **kwargs):
if created:
custom_user, created = CustomUser.objects.get_or_create(user=instance)
post_save.connect(create_custom_user, sender=User)
As for the second part, theres already a change password form in the admin. To filter out the displayed fields you can create a CustomUserAdmin and register it together with the model. It's pretty self explaining in the django docs.
django docs: list_display
In Django admin site, when listing all the objects for a given model, I know we can customize which columns get displayed for a ModelA via list_display
Say that ModelA has a one-to-many relationship with ModelB. I would like to add another column on the listing page for ModelA, where each entry is a URL pointing to all objects of ModelB having a foreign key relationship on corresponding instance of Model A in that row. How can I achieve this customization with the admin app?
you should add a method to ModelA's admin class:
def modelb_link(self, inst):
url = u'../modelb/?modela__id__exact=%d' % inst.id
return u'Models B' % url
modelb_link.allow_tags = True
modelb_link.short_description = u'Models B'
In the url the 'modela__id__exact' part is a filter for list page, where 'modela' is the name of ForeignKey field, in ModelB class, that links to ModelA.
Then use this method in 'list_display' property and you are done. If you encounter any problems, just ask, I'll try to help.
Greetings,
Lukasz