The invoice_date field on one of my models is not showing up in my admin.
invoice_date = models.DateField(auto_now=True, auto_now_add=False, null=True, blank=True)
I've tried the following:
renaming the field
running makemigrations/migrations
restarting the server
deleting cookies
I can successfully write to the field and pull data from it, so it appears to be there and functioning as desired. I just can't see it in the admin.
I have no special code in my admin.py. I've just registered the model
admin.py
from userorders.models import UserCartItem
admin.site.register(UserCart)
Any suggestions are welcome! Thanks!
Accoring to Django's documentation:
As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set.
You can circumvent this by explicitly defining it on the ModelAdmin class:
from userorders.models import UserCartItem
class UserCartItemAdmin(admin.ModelAdmin):
list_display = ['invoice_date']
fields = ['invoice_date']
# if you want the field just to visible but not editable
# readonly_fields = ['invoice_date']
admin.site.register(UserCartItem, UserCartItemAdmin)
You could try something following-
from userorders.models import UserCartItem
class UserCartItemAdmin(admin.ModelAdmin):
list_display = ['field_name_1', 'field_name_2', 'invoice_date']
admin.site.register(UserCartItem, UserCartItemAdmin)
Related
I have 9 Million records.
It will be 200M soon.
It can take 15min + to fetch this:
class Follower():
hashtags = models.ManyToManyField(
"instagram_data.Hashtag", verbose_name=_("hashtags_name"))
class Hashtag(models.Model):
name = models.CharField(_("HashtagName"), max_length=150, null=True, blank=True, unique=True)
def __str__(self):
return self.name
Ubunto htop:
I think it reverse lookup for all values.
I think it will have maybe 2000 records found.
What am I doing wrong ?
The image you post looks like the model admin, if that is the case try adding the field hashtags to the raw_id_fields [Django docs] of the model admin (although then you won't get the select tag and will have to manually enter the id/pk) or the autocomplete_fields [Django docs] which would use select2 to load the choices asychronously. This would work something like:
class HashtagAdmin(admin.ModelAdmin):
ordering = ['name']
search_fields = ['name']
class FollowerAdmin(admin.ModelAdmin):
autocomplete_fields = ['hashtags']
admin.site.register(Follower, FollowerAdmin)
admin.site.register(Hashtag, HashtagAdmin)
If this is not in the model admin you can change the widget you use in the form on the form class (you will need to build a custom widget or look for some package that provides widget that can allow comma separted values), or you can use the package Django-Select2 to give the user a searchable select tag (again using select2).
I have the following code in my admin.py:
class UserManagedGroupAdmin(admin.ModelAdmin):
inlines = [MembershipInline]
search_fields = ('name', 'leader__username', )
list_display = ('__unicode__', 'leader', )
filter_horizontal = ('permissions', )
raw_id_fields = ('leader', )
admin.site.register(UserManagedGroup, UserManagedGroupAdmin)
The magnifying glass icon for searching doesn't appear in the admin page.
This is what I'm getting:
As you can see it's showing the unicode method of the model instead of the search icon I want.
Field 'leader' is a ForeignKey to User.
Could it be that django disables the search for ForeignKeys to User for security reasons, or am I doing something wrong?
The widget would be perfect for choosing users... I mean, I can't leave a huge select there with every user of my site.
Thanks.
I've found the problem thanks to this message in django-users.
I had to register in the admin the model to which the ForeignKey points to.
The search doesn't work without that.
Hi encounter the same issue but reason's a bit different.
To integrate the User and UserGroup with another app's admin (e.g. some_app)
I added below code to some_app/admin.py
class ProxyUser(User):
class Meta:
proxy = True
verbose_name = User._meta.verbose_name
verbose_name_plural = User._meta.verbose_name_plural
class ProxyGroup(Group):
class Meta:
proxy = True
verbose_name = Group._meta.verbose_name
verbose_name_plural = Group._meta.verbose_name_plural
admin.site.unregister(Group)
admin.site.unregister(User)
admin.site.register(ProxyGroup)
admin.site.register(ProxyUser, UserAdmin)
I think the unregister(...) will affect the other app's admin Globally!
That's another cause of missing search icon.
In the admin.py file add:
admin.site.register(YourModel)
This did the trick, Where YourModel is the model to be displayed with the magnifying glass
How does one change the slug for an existing object? I have two objects with conflicting slugs and I need to change one to fix the problem. Is there a way to do this in the admin interface? I'm not seeing one.
You need to make sure that a) The appropriate app is registered to appear in the admin interface, and b), the slug is set to be included. In admin.py of the relevant app, you need to make sure that slug is in the fields attribute:
admin.py
class MyModelAdmin(admin.ModelAdmin):
fields = ( ... , 'slug', ... )
admin.site.register(MyModel, MyModelAdmin)
furthremore, you should try to avoid getting into a situation where two records have the same slug. You can do this by adding unique=True to the model field.
models.py
class MyModel(models.Model):
...
slug = models.CharField(max_length=128, unique=True)
If there are problems with the above, you will have to manually change one of the slugs in the DB or use the shell:
> manage.py shell
> from myapp.models import MyModel
> obj = MyModel.objects.get(id=0) # or whatever the id is for the problematic obj
> obj.slug = "new-slug"
> obj.save()
Is there a Djangotastic way to display a default value for a field in the admin when there isn't a value? Like 'n/a', but not to save that to the database?
When I set all the fields in the model below to readonly in the admin, the front-end display looks like the image at the bottom. It feels visually collapsed like it should have a value or a box or something. If there isn't an easy way to do what I am looking for, then is there another solution to make the front-end admin more clear for the user?
class Package(models.Model):
packaging_format = models.CharField(max_length=40)
package_delivery_pattern = models.CharField(max_length=30, blank=True)
package_delivery_comments = models.CharField(max_length=250, blank=True)
package_manifest_filename = models.CharField(max_length=50)
package_description = models.CharField(max_length=250, blank=True)
package_naming_pattern = models.CharField(max_length=50)
Screenshot of fields as displayed in the admin:
What's happening is that your actually saving a empty string '' in your CharFields instead of None values (because of the blank=True). So the Django-admin is showing the string you saved in the db (in this case, nothing).
If you change your CharFields to null=True instead of blank=True, you will be saving NULL in your database instead of an empty string. And that way, you will get the behaviour you want.
EDIT: I know this solution is not recommended (following Django Docs), but that's the behaviour you wanted. Django-admin is just showing you the string you have in the database, which is ''.
Another solution that comes to my mind is to modify the ModelAdmin for your Package model, something like:
class PackageAdmin(admin.ModelAdmin):
readonly_fields = ['show_package_delivery_pattern', ...]
def show_package_delivery_pattern(self, obj):
if obj.package_delivery_pattern:
return obj.package_delivery_pattern
else:
return 'N/A'
# same with all your CharFields..
As of Django 1.9 you can use empty_value_display at the site, model, or field level in the Django admin. At the model level:
class YourModelAdmin(admin.ModelAdmin):
empty_value_display = '---'
I have created two models out of an existing legacy DB , one for articles and one for tags that one can associate with articles:
class Article(models.Model):
article_id = models.AutoField(primary_key=True)
text = models.CharField(max_length=400)
class Meta:
db_table = u'articles'
class Tag(models.Model):
tag_id = models.AutoField(primary_key=True)
tag = models.CharField(max_length=20)
article=models.ForeignKey(Article)
class Meta:
db_table = u'article_tags'
I want to enable adding tags for an article from the admin interface, so my admin.py file looks like this:
from models import Article,Tag
from django.contrib import admin
class TagInline(admin.StackedInline):
model = Tag
class ArticleAdmin(admin.ModelAdmin):
inlines = [TagInline]
admin.site.register(Article,ArticleAdmin)
The interface looks fine, but when I try to save, I get:
Warning at /admin/webserver/article/382/
Field 'tag_id' doesn't have a default value
This can also happen if you have a disused field in your database that doesn't allow NULL.
The problem was that in the DB, tag_id wasn't set as an autoincrement field.
What solved this issue in my case was disabling STRICT_TRANS_TABLES SQL mode which was enabled by default.