Change Django slug for an existing object? - django

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()

Related

Model field missing in admin

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)

admin.py: "model = Thing" ,what does this code mean?if without it what gonna happen?

every one,,I am reading a Django practice book,,I saw a code "model = Thing" in admin.py,,,however, when I remove "model = Thing",,,the web program still can run,the admin site looks no difference??,what does this code mean?if without it what gonna happen? my models.py class is Thing
admin.py
from django.contrib import admin
from collection.models import Thing
class ThingAdmin(admin.ModelAdmin):
model = Thing #if I remove this code, the program still can run,,why need this code
list_display = ('name', 'description',)
prepopulated_fields = {'slug': ('name',)}
admin.site.register(Thing, ThingAdmin)
modles.py
from django.db import models
class Thing(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
slug = models.SlugField(unique=True)
Setting a model attribute on the ModelAdmin class will have no effect. You can safely remove that line from your code.
In the Django admin, you specify the model when you call admin.site.register(), or by using the register decorator. This allows you to use the same model admin class for more than one model.
admin.site.register(Thing, ThingAdmin)
admin.site.register(OtherThing, ThingAdmin)
As Jon pointed out in the comments, you do need to specify the model for InlineModelAdmin objects.

django admin gives warning "Field 'X' doesn't have a default value"

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.

Django admin: How to display a field that is marked as editable=False' in the model?

Even though a field is marked as 'editable=False' in the model, I would like the admin page to display it. Currently it hides the field altogether.. How can this be achieved ?
Use Readonly Fields. Like so (for django >= 1.2):
class MyModelAdmin(admin.ModelAdmin):
readonly_fields=('first',)
Update
This solution is useful if you want to keep the field editable in Admin but non-editable everywhere else. If you want to keep the field non-editable throughout then #Till Backhaus' answer is the better option.
Original Answer
One way to do this would be to use a custom ModelForm in admin. This form can override the required field to make it editable. Thereby you retain editable=False everywhere else but Admin. For e.g. (tested with Django 1.2.3)
# models.py
class FooModel(models.Model):
first = models.CharField(max_length = 255, editable = False)
second = models.CharField(max_length = 255)
def __unicode__(self):
return "{0} {1}".format(self.first, self.second)
# admin.py
class CustomFooForm(forms.ModelForm):
first = forms.CharField()
class Meta:
model = FooModel
fields = ('second',)
class FooAdmin(admin.ModelAdmin):
form = CustomFooForm
admin.site.register(FooModel, FooAdmin)
Add the fields you want to display on your admin page.
Then add the fields you want to be read-only.
Your read-only fields must be in fields as well.
class MyModelAdmin(admin.ModelAdmin):
fields = ['title', 'author', 'published_date', 'updated_date', 'created_date']
readonly_fields = ('updated_date', 'created_date')
You could also set the readonly fields as editable=False in the model (django doc reference for editable here). And then in the Admin overriding the get_readonly_fields method.
# models.py
class MyModel(models.Model):
first = models.CharField(max_length=255, editable=False)
# admin.py
class MyModelAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
return [f.name for f in obj._meta.fields if not f.editable]
With the above solution I was able to display hidden fields for several objects but got an exception when trying to add a new object.
So I enhanced it like follows:
class HiddenFieldsAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
try:
return [f.name for f in obj._meta.fields if not f.editable]
except:
# if a new object is to be created the try clause will fail due to missing _meta.fields
return ""
And in the corresponding admin.py file I just had to import the new class and add it whenever registering a new model class
from django.contrib import admin
from .models import Example, HiddenFieldsAdmin
admin.site.register(Example, HiddenFieldsAdmin)
Now I can use it on every class with non-editable fields and so far I saw no unwanted side effects.
You can try this
#admin.register(AgentLinks)
class AgentLinksAdmin(admin.ModelAdmin):
readonly_fields = ('link', )

Django: list_filter and foreign key fields

Django doesn't support getting foreign key values from list_display or list_filter (e.g foo__bar). I know you can create a module method as a workaround for list_display, but how would I go about to do the same for list_filter? Thanks.
Django supports list_filter with foreign key fields
# models.py:
class Foo(models.Model):
name = models.CharField(max_length=255)
def __unicode__(self):
return self.name
class Bar(models.Model):
name = models.CharField(max_length=255)
foo = models.ForeignKey(Foo)
# admin.py:
class BarAdmin(admin.ModelAdmin):
list_filter = ('foo__name')
From documentation: Field names in list_filter can also span relations using the __ lookup
Well, the docs say that you can may use ForeignKey field types in list_filter:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
An example:
# models.py:
class Foo(models.Model):
name = models.CharField(max_length=255)
def __unicode__(self):
return self.name
class Bar(models.Model):
name = models.CharField(max_length=255)
foo = models.ForeignKey(Foo)
# admin.py:
class BarAdmin(admin.ModelAdmin):
list_filter = ('foo')
If you want to filter by a field from the related model, there's a patch on the way to make this work (will probably be merged into 1.2 as it seems):
http://code.djangoproject.com/ticket/3400
solution from this page worked for me http://www.hoboes.com/Mimsy/hacks/fixing-django-124s-suspiciousoperation-filtering/
define
class SmarterModelAdmin(admin.ModelAdmin):
valid_lookups = ()
def lookup_allowed(self, lookup, *args, **kwargs):
if lookup.startswith(self.valid_lookups):
return True
return super(SmarterModelAdmin, self).lookup_allowed(lookup, *args, **kwargs)
then allow the lookup for certain foreign key field
class PageAdmin(SmarterModelAdmin):
valid_lookups = ('parent')
If you construct the URL for the changelist manually then Django has no problems following relationships. For example:
/admin/contact/contact/?participant__event=8
or
/admin/contact/contact/?participant__event__name__icontains=er
Both work fine (although the latter doesn't add 'distinct()' so might have duplicates but that won't usually be an issue for filters)
So you just need to add something to the page that creates the correct links. You can do this either with by overriding the changelist template or by writing a custom filterspec. There are several examples I found by Googling - particularly on Django Snippets
You can easily create custom filters since Django 1.4 by overriding django.contrib.admin.SimpleListFilter class.
More information :
Admin list_filter documentation ;
Django-1.4 release note.
I ran into the same problem and really needed a solution. I have a workaround that lets you create a filter on a FK related model property. You can even traverse more than one FK relationship. It creates a new FilterSpec subclass that subclasses the default RelatedFilterSpec used to give you a filter on a ForeignKey field.
See http://djangosnippets.org/snippets/2260/
The Haes answer works perfectly, but if the __ looks up to another ForeignKey field, you end up with a blank result. You must place another __ lookup, until it points to the real field.
In my case: list_filter = ('place__condo__name', )
my models.py:
class Condo(models.Model):
name = models.CharField(max_length=70)
...
class Place(models.Model):
condo = models.ForeignKey(Condo)
...
class Actions(models.Model):
place = models.ForeignKey(Place)
...