Django :::表格中的S ..

时间:2015-04-20 00:47:06

标签: python django forms django-models

请帮助我。我在表格中遇到了麻烦。 我从模型中创建表单。 这是models.py

class hire_article(models.Model):
hiring = models.CharField(max_length=50)
slug = models.SlugField(max_length=200)

这是表格

from django import forms
from django.forms import ModelForm, Textarea
from django.utils.translation import ugettext_lazy as _
import datetime
from .models import hire_article
from django.template.defaultfilters import slugify
from django.db import models
from django.contrib import admin

class postform(ModelForm):

    # class hire_articleAdmin(admin.ModelAdmin):
    #     prepopulated_fields = {"slug": ("hiring",)}
    class Meta:
        # more_infomation = MyFormField(max_length=200, required=False,
        #                    help_text="Use puns liberally")
        model = hire_article
        # exclude = ['slug']
        fields = '__all__'

这是admin.py

 from django.contrib import admin
    from .models import category, hire_article, localwork, salary,         mode_work, exp_year, degree, sex, language

class hire_articleAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("hiring",)}
    list_display = ('id', 'hiring', 'publication_date', 'namecompany', )
    admin.site.register(hire_article, hire_articleAdmin)

仅适用于管理员。我希望它也可以用于表格。提前谢谢!

1 个答案:

答案 0 :(得分:1)

在models.py中进行以下更改,并在之前的comments.py

中的注释中进行更改建议
from django.template.defaultfilters import slugify

class hire_article(models.Model):
    hiring = models.CharField(max_length=50)
    slug = models.SlugField(max_length=200)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.hiring)
        super(hire_article, self).save(*args, **kwargs)

ps:不推荐您的模型命名约定。查看PEP8 https://www.python.org/dev/peps/pep-0008/#class-names