I want to hide a column from django admin and set its value from session.
In simple words i want to set a property of a model from session and don't want a field in admin for that column.
Can someone help me?
You can mark the field as read-only but still have it visible in the admin, or completely exclude it.
class MyModel(models.Model):
field1 = models.CharField(max_length=20) # this is editable
field2 = models.CharField(max_length=20, editable=False) # this is not
or
#admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
exclude = ['field2']
If you're using a custom model form in the admin, just don't include the field in the fields attribute.
As far as I remember there are a lot of ways to do this, with the fields attribute on Meta you can hide or select which fields to show.
check This example
Related
In my models.py my model store contains name, brand_name fields
Now,I want to create new field called brand_type in store model dynamically from Django admin,how can I do this?
Can we change schema of model using SchemaEditor? How?
you can't create model's fields via admin panel
you must create fields in models.py
admin panel is only a convinient way to manipulate your database, but not modifiyng its tables
It requires migration while the server is running. Create brand_type field under your model and set blank=True
In your models.py
class Store(models.Model):
name = ...
brand_name = ...
brand_type = models.CharField(max_length = 40, blank= True, null =true)
In your console
./manage.py makemigrations
./manage.py migrate
Extra Stuff:
If you are interested in Text Choices
or
If you wanna make it more dynamic based on your use case,
create a model called BrandType and link it in Store using
brand_type = models.ForeignKey(BrandType,on_delete=models.PROTECT)
models.py
class BrandType(models.Model):
name = ...
some_other fields=
#... Store model follows
admin.py
# other stuff
from .models import BrandType
admin.site.register(BrandType)
Take away: It is not advisable to modify your models.py file using admin directly it will cause integrity issues and there are better ways to achieve your desired functionality.
DateTimeField(auto_now_add=True, null=True) in my django model.py file and everything is working fine but the problem is it won't show data/time in django admin panel.
you need to add that field to list_display:
in admin.py:
class PostAdmin(admin.ModelAdmin): # "PostAdmin" is used for e.g purposes
[..]
list_display = (.., 'created_at',)
[..]
when you set:
DateTimeField(auto_now_add=True, null=True)
it will add your object that moment that you post the object, so if you want to select manually, you have to set:
DateTimeField(auto_now_add=False, null=True)
then it will show your Date field in your admin panel model.
Your date modified field may not display when editing an object since it is a read only value. In admin.py, try setting readonly_fields = ("date_modified",), replacing date_modified with whatever you named your DateTimeField
On Django Rest, I have a many to many relation for a field of material list. Instances of materials are added in a loan model. It works, to add through the admin site but the display is disintuitive: Instances are mixed with materials (instances are in parenthesis).
It's possible to have to separated list ? One to select materials and one other to add/remove instances linked to materials ?
The name that it shows in the list are the results of the __str__ method on your model. So if you want to display something else, you can alter the __str__ method. For example if this is a model Material with a name field, you can set it to:
class Material(models.Model):
name = models.CharField(max_length=128, unique=True)
def __str__(self):
return self.name
You can set the filter_horizontal attribute [Django-doc]:
from django.contrib import admin
#admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
# …
filter_horizontal = ('materials_field_name',)
this thus will use a widget with two lists where you can move to the right and the left.
I have a user, admin and employee models, both employee and admin are related to the user model with a oneToOne field what i would like to do is in the admin form i have one field "user"
instead of having a drop down field with all users i would like to have a search box so that when i search for a specific user i find him and choose him to be an admin.
how?
i also tried to add user's fields to admin's form but i couldn't
i tried the inline thing, the parent_link in the admin's model... but i couldn't find a solution
Now i would like to minimise the search through the long users list by adding a search box to the field
I have solved this type of problem using django-ajax-selects package. Hope this will help you.
app/admin.py:
from ajax_select import register, LookupChannel
#register('users')
class UsersLookup(LookupChannel):
model = User
def get_query(self, q, request):
return self.model.objects.filter(username__icontains=q)
class EmployeeAdminForm(forms.ModelForm):
user = AutoCompleteSelectField('users', required=False,
help_text=None)
class Meta:
model = Employee
fields = '__all__'
settings.py
AJAX_LOOKUP_CHANNELS = {
'users' : {'model': 'auth.user', 'search_field': 'username'},
}
Use django's raw_id_fields. With that you can declare a foreign key object searchable.
My model looks like this:
class Asset(models.Model):
serial_number = models.CharField(max_length=100, unique=True)
asset_tag = models.CharField(max_length=100, unique=True)
class WorkOrder(models.Model):
asset = models.ForeignKey(Asset)
Essentially, a work order is submitted and then an admin assigns an asset to the work order. The asset_tag field is a barcode that we can scan in. When editing the work order in the Django admin, by default the asset field is displayed as a <select> widget. What we want to be able to do is have a search field so that we can scan the asset tag and then search for the right asset in the DB to associate with the work order.
I know you can customize the Django admin foreign key to a hard coded query, but I can't figure out how to get it so it does a search based on a field on the admin page.
Did you take a look at raw_id_fields?
It should be pretty to close to what you're after.
If you are using Django >= 2.0, you can take advantage of a feature called autocomplete_fields. You must define search_fields on the related object’s ModelAdmin because the autocomplete search uses it.
Since you have a ForeignKey relationship to Asset in WorkOrder, in the admin.py of your app add the following:
from django.contrib import admin
#admin.register(Asset)
class AssetAdmin(admin.ModelAdmin):
search_fields = ["serial_number", "asset_tag"]
#admin.register(WorkOrder)
class WorkOrderAdmin(admin.ModelAdmin):
autocomplete_fields = ["asset"]
Add the fields you want to use for searching to search_fields, and add define autocomplete_fields as shown in the code above.
Now you can use the autocomplete_fields from django 2.0.
It's quite neat.