Django:管理员字段不可编辑?

时间:2018-03-22 20:24:06

标签: django django-models django-admin

我从Django builder导入了一个项目,设法安装了依赖项,并启动了管理员,但我遇到了问题。

管理员中的所有字段都不可编辑!他们都不是。

以下是该问题的屏幕截图:

Django Admin No edit

以下是相关文件: Admin.py

from django.contrib import admin
from django import forms
from .models import Tours, Locations, Photos, PlaceLocations, Information

class ToursAdminForm(forms.ModelForm):

    class Meta:
        model = Tours
        fields = '__all__'


class ToursAdmin(admin.ModelAdmin):
    form = ToursAdminForm
    list_display = ['name', 'slug', 'created', 'last_updated', 'Tags', 'Photo', 'Description', 'Duration']
    readonly_fields = ['name', 'slug', 'created', 'last_updated', 'Tags', 'Photo', 'Description', 'Duration']

admin.site.register(Tours, ToursAdmin)


class LocationsAdminForm(forms.ModelForm):

    class Meta:
        model = Locations
        fields = '__all__'


class LocationsAdmin(admin.ModelAdmin):
    form = LocationsAdminForm
    list_display = ['name', 'slug', 'created', 'last_updated', 'Photo', 'MainLocLat', 'MainLocLon', 'MainLocRad', 'Audio', 'Description']
    readonly_fields = ['name', 'slug', 'created', 'last_updated', 'Photo', 'MainLocLat', 'MainLocLon', 'MainLocRad', 'Audio', 'Description']

admin.site.register(Locations, LocationsAdmin)


class PhotosAdminForm(forms.ModelForm):

    class Meta:
        model = Photos
        fields = '__all__'


class PhotosAdmin(admin.ModelAdmin):
    form = PhotosAdminForm
    list_display = ['name', 'slug', 'created', 'last_updated', 'Photo']
    readonly_fields = ['name', 'slug', 'created', 'last_updated', 'Photo']

admin.site.register(Photos, PhotosAdmin)


class PlaceLocationsAdminForm(forms.ModelForm):

    class Meta:
        model = PlaceLocations
        fields = '__all__'


class PlaceLocationsAdmin(admin.ModelAdmin):
    form = PlaceLocationsAdminForm
    list_display = ['name', 'slug', 'created', 'last_updated', 'Latitude', 'Longitude', 'Radius']
    readonly_fields = ['name', 'slug', 'created', 'last_updated', 'Latitude', 'Longitude', 'Radius']

admin.site.register(PlaceLocations, PlaceLocationsAdmin)


class InformationAdminForm(forms.ModelForm):

    class Meta:
        model = Information
        fields = '__all__'


class InformationAdmin(admin.ModelAdmin):
    form = InformationAdminForm
    list_display = ['name', 'slug', 'created', 'last_updated', 'Photo', 'Description']
    readonly_fields = ['name', 'slug', 'created', 'last_updated', 'Photo', 'Description']

admin.site.register(Information, InformationAdmin)

models.py

from django.core.urlresolvers import reverse
from django_extensions.db.fields import AutoSlugField
from django.db.models import *
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth import get_user_model
from django.contrib.auth import models as auth_models
from django.db import models as models
from django_extensions.db import fields as extension_fields

from smartfields import fields
from smartfields.dependencies import FileDependency
from smartfields.processors import ImageProcessor


class Tours(models.Model):

    # Fields
    name = models.CharField(max_length=255)
    slug = extension_fields.AutoSlugField(populate_from='name', blank=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    Tags = models.TextField(max_length=100)
    Photo = models.ImageField(upload_to="/upload/images/")
    Description = models.TextField(max_length=2000)
    Duration = models.IntegerField()


    class Meta:
        ordering = ('-created',)

    def __unicode__(self):
        return u'%s' % self.slug

    def get_absolute_url(self):
        return reverse('app_name_tours_detail', args=(self.slug,))


    def get_update_url(self):
        return reverse('app_name_tours_update', args=(self.slug,))


class Locations(models.Model):

    # Fields
    name = models.CharField(max_length=255)
    slug = extension_fields.AutoSlugField(populate_from='name', blank=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    Photo = models.ImageField(upload_to="/upload/images/")
    MainLocLat = models.TextField(max_length=100)
    MainLocLon = models.TextField(max_length=100)
    MainLocRad = models.TextField(max_length=100)
    Audio = models.FileField(upload_to="/upload/files/")
    Description = models.TextField(max_length=2000)

    # Relationship Fields
    Photos = models.ForeignKey('app_name.Photos', )
    PlaceLocations = models.ForeignKey('app_name.PlaceLocations', )

    class Meta:
        ordering = ('-created',)

    def __unicode__(self):
        return u'%s' % self.slug

    def get_absolute_url(self):
        return reverse('app_name_locations_detail', args=(self.slug,))


    def get_update_url(self):
        return reverse('app_name_locations_update', args=(self.slug,))


class Photos(models.Model):

    # Fields
    name = models.CharField(max_length=255)
    slug = extension_fields.AutoSlugField(populate_from='name', blank=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    Photo = models.ImageField(upload_to="/upload/images/")


    class Meta:
        ordering = ('-created',)

    def __unicode__(self):
        return u'%s' % self.slug

    def get_absolute_url(self):
        return reverse('app_name_photos_detail', args=(self.slug,))


    def get_update_url(self):
        return reverse('app_name_photos_update', args=(self.slug,))


class PlaceLocations(models.Model):

    # Fields
    name = models.CharField(max_length=255)
    slug = extension_fields.AutoSlugField(populate_from='name', blank=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    Latitude = models.TextField(max_length=100)
    Longitude = models.TextField(max_length=100)
    Radius = models.IntegerField()


    class Meta:
        ordering = ('-created',)

    def __unicode__(self):
        return u'%s' % self.slug

    def get_absolute_url(self):
        return reverse('app_name_placelocations_detail', args=(self.slug,))


    def get_update_url(self):
        return reverse('app_name_placelocations_update', args=(self.slug,))


class Information(models.Model):

    # Fields
    name = models.CharField(max_length=255)
    slug = extension_fields.AutoSlugField(populate_from='name', blank=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    Photo = models.ImageField(upload_to="/upload/images/")
    Description = models.TextField(max_length=2000)


    class Meta:
        ordering = ('-created',)

    def __unicode__(self):
        return u'%s' % self.slug

    def get_absolute_url(self):
        return reverse('app_name_information_detail', args=(self.slug,))


    def get_update_url(self):
        return reverse('app_name_information_update', args=(self.slug,))

1 个答案:

答案 0 :(得分:1)

您最好删除每个readonly_fields课程的ModelAdmin属性。这样做,管理面板中的字段将不是只读的!