33👍
Add a __unicode__()
method to Host
. To show the primary key of your host objects, you’d want something like:
class Host(models.Model):
host = models.CharField(max_length=100, primary_key=True)
def __unicode__(self):
return self.pk
...
You might want to think about showing the contents of the host
field:
class Host(models.Model):
host = models.CharField(max_length=100, primary_key=True)
def __unicode__(self):
return self.host
...
You’ll need to do something similar for every model you’ve got.
For Python 3 compatibility, you’ll want to do something like this (see the documentation):
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Host(models.Model):
host = models.CharField(max_length=100, primary_key=True)
def __str__(self):
return self.host
...
10👍
contrib.admin
has been reworked in 1.0, and old Admin
classes inside models no longer work. What you need is ModelAdmin
subclass in your_application.admin
module, e.g.
from your_application.models import Host
from django.contrib import admin
class HostAdmin(admin.ModelAdmin):
list_display = ('host',)
admin.site.register(Host, HostAdmin)
Or use __unicode__
in the model itself, e.g.
class Host(models.Model):
host = models.CharField(max_length=100,primary_key=True)
def __unicode__(self):
return self.host
- Extending custom router to default router across apps in Django Rest Framework
- Using urls names in views
- Django JSON De-serialization Security
- Django Rest Framework 3 Serializers on non-Model objects?
3👍
It might also be worth mentioning that, if you are using an auto-incrementing primary key for your models, you will need to coerce it into a string, eg:
def __unicode__(self):
return str(self.pk)
- Django's annotate Count with division returns integer instead of float
- Django-mptt and multiple parents?
- Is it possible to serve a static html page at the root of a django project?
- Using django how can I combine two queries from separate models into one query?
Source:stackexchange.com