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
Related
I need help to get existing questions from the field question_name into the field dependent_question (with dropdown) from the Questions model class. It will help user to select dependent question when they add any new question from django admin panel.
# model.py
class Questions(models.Model):
question_id = models.AutoField(primary_key=True)
question_name = models.CharField(max_length=300)
dependent_question = models.CharField(max_length=300,blank=True, null=True)
Take a look at this post: https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html
It shows you how to achieve that using javascript. I have done this, and it works great.
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
I have date field which is editable=False.
The field is populated with datetime.now() at creation time.
Since admin will not show editable=False fields, I created a custom admin.
The custom admin uses a form with this field:
date = forms.DateTimeField(widget=widgets.AdminSplitDateTime, required=False)
What I want to achieve is this:
users will not be able to touch this field (hence, editable=False)
Admin will be able to change the field's value but won't be forced to (this is why I have required=False)
Admin will be able to see the current value of the field.
I'm failing to achieve (3). I create an entry, and I see that it has a valid date when I look in the database. But when I open it from admin panel, the date widget is empty.
Any ideas how to make the date widget show the current value of the field?
First, models.DateTimeField uses widgets.AdminSplitDateTime by default in the Admin, you don't have to specify it explicitly.
For normal staff, use readonly_fields to prevent the date field from being changed.
For administrator, render a different changing form to allow the modification towards the date field. In Django 1.4, it can be easily done by overriding ModelAdmin.get_readonly_fields():
class YourModelAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
# we don't use self.readonly_fields anymore
if request.user.is_superuser:
return ()
return ['date']
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.
Python 2.7
Django 1.3
When I include 'user_id','user' in the admin.py, then...
no user field shows up in the form when I click to add a Timeslip.
If I submit it anyway, then it shows the user field with a "This field is required." error message.
If I pick a user & submit again, then I get "'TimeslipAdmin.fields' refers to field 'user_id' that is missing from the form." even though 'user_id' is clearly listed in my admin.py (see below)
The Traceback says --
Exception Type: ImproperlyConfigured at /admin/timeslip/timeslip/add/
Exception Value: 'TimeslipAdmin.fields' refers to field 'user_id' that is missing from the form.
But...if I leave 'user_id','user' out of the admin.py then....
no user field shows up when I click to add a Timeslip.
Submit it anyway, and it shows the user field & a "Timeslip with this User already exists." error message. (which shouldn't be an error either 'cause I want users to have multiple Timeslip's which means another error I'll have to figure out once I can just get this form working)
admin.py
from timeslip.models import Timeslip
from django.contrib import admin
class TimeslipAdmin(admin.ModelAdmin):
fields = ['user_id','user','day','hours_as_sec','part_of_day','drove','gas_money','notes']
admin.site.register(Timeslip, TimeslipAdmin)
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Timeslip(models.Model):
user=models.ForeignKey(User)
day = models.DateField()
hours_as_sec = models.PositiveIntegerField()
part_of_day = models.CharField(max_length=16,choices=PART_O_DAY)
drove = models.BooleanField(default=False)
gas_money = models.DecimalField(max_digits=5,decimal_places=2)
notes = models.TextField()
class UserProfile(models.Model):
user = models.ForeignKey(User)
url = models.URLField("Website", blank=True)
position = models.CharField(max_length=50, blank=True)
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
I'm very clueless how to overcome this. I'm coming from a PHP background, a newbie to Python & Django.
user_id and user are redundant. Django automatically names your user field user_id in the database (since it holds the id for the User instance it points to.)
Change your admin.py to the following and it should work:
class TimeslipAdmin(admin.ModelAdmin):
fields = ['user','day','hours_as_sec','part_of_day','drove','gas_money','notes']
Also you're including all of the fields in the admin, so you really don't need to specify the fields. This would work just as well:
class TimeslipAdmin(admin.ModelAdmin):
pass
But...if I leave 'user_id','user' out of the admin.py then...
You haven't got 'user_id' field. So delete only this field and I think everything will work fine.