Reputation: 5609
I am trying to get the pub_date
and the update
option in the admin. But there's no such thing. In the admin there's only field for username
, password
and client ip
. What am I missing? Your help will be very much appreciated. Thank you.
models:
class Member(models.Model):
username = models.CharField(max_length=150)
password = models.CharField(max_length=150)
pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
update = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self):
return self.username
class IP(models.Model):
client_ip = models.CharField(max_length=50)
pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
update = models.DateTimeField(auto_now_add=False, auto_now=True)
Update:
from django.contrib import admin
from models import Member, IP
# Register your models here.
admin.site.register(Member)
admin.site.register(IP)
Upvotes: 3
Views: 1924
Reputation: 2242
This is read-only field, so it is ignored by default in Django Admin. To show it, you have to define custom ModelAdmin
class and specify it in fields
atribute.
from django.contrib import admin
from models import Member, IP
class MemberAdmin(admin.ModelAdmin):
fields = ('username', 'password', 'pub_date', 'update')
class IPAdmin(admin.ModelAdmin):
fields = ('client_ip', 'pub_date', 'update')
admin.site.register(Member, MemberAdmin)
admin.site.register(IP, IPAdmin)
You can use fieldsets
instead of fields
to display fields in more sections (click).
I'm not aware of any options where you don't have to specify all fields to display.
PS: You can omit auto_now_add = False
and auto_now = False
as they are set to False
by default.
Upvotes: 2
Reputation: 45585
@Selcuk is right, there is not much sense of editing the automatic fields, so django excludes datetime fields with auto_now*
arguments from the admin.
If you really want to see these fields in admin then you have to change the model to mimic the auto_now*
behavior:
from django.utils import timezone
class Member(models.Model):
username = models.CharField(max_length=150)
password = models.CharField(max_length=150)
pub_date = models.DateTimeField(default=timezone.now)
update = models.DateTimeField(default=timezone.now)
def save(self, *args, **kwargs):
if self.pk is None:
self.pub_date = timezone.now()
else:
kwargs['update_fields'] = ['username', 'password', 'update']
self.update = timezone.now()
return super(Member, self).save(*args, **kwargs)
But note that if you will try to edit the fields your changes will be ignored.
Upvotes: 1