English 中文(简体)
模型中的Django表格
原标题:Django Forms from Models

我试图整合一个名为的应用程序, https://github.com/saebyn/django-credits" rel=“不跟随 noreferrer” >Django-Classififics , 但是在如何操控某些元素方面有困难 。

应用程序允许您通过管理部分创建一个类别和子字段, 该字段将被显示, 并允许用户在上述类别中创建一个职位 。 虽然 Django 管理允许您创建字段 :

“https://i.sstatic.net/5ozFt.png' alt=“DJango行政官”/>

并写到数据库:

""https://i.sstatic.net/siZ0b.png" alt="此处输入图像描述"/"

我无法找到如何调用这些字段并将其显示给用户。 目前, 只有标题字段被显示 。 我理解使用模型格式会允许我访问这些元素, 但我不确定如何将其与 BaseForm 整合起来( 在下面的应用程序中使用 ) :

class AdForm(BaseForm):
  def __init__(self, instance, data=None, files=None, auto_id= id_%s , prefix=None,
                         initial=None, error_class=ErrorList, label_suffix= : ,
                         empty_permitted=False):

    self.instance = instance
    object_data = ad_to_dict(self.instance)
    self.declared_fields = SortedDict()
    self.base_fields = fields_for_ad(self.instance)

    # if initial was provided, it should override the values from instance
    if initial is not None:
        object_data.update(initial)

    BaseForm.__init__(self, data, files, auto_id, prefix, object_data,
                      error_class, label_suffix, empty_permitted)

  def save(self, commit=True):
    if not commit:
        raise NotImplementedError("AdForm.save must commit it s changes.")

    if self.errors:
        raise ValueError("The ad could not be updated because the data didn t validate.")

    cleaned_data = self.cleaned_data

    # save fieldvalues for self.instance
    fields = field_list(self.instance)

    for field in fields:
        if field.enable_wysiwyg:
            value = unicode(stripHTML(cleaned_data[field.name]))
        else:
            value = unicode(cleaned_data[field.name])

        # strip words in settings.FORBIDDEN_WORDS
        for word in settings.FORBIDDEN_WORDS:
            value = value.replace(word,   )

        # title is stored directly in the ad, unlike all other editable fields
        if field.name ==  title :
            self.instance.title = value
            self.instance.save()
        else:
            # check to see if field.fieldvalue_set has a value with ad=self.instance
            try:
                # if it does, update
                fv = field.fieldvalue_set.get(ad=self.instance)
            except FieldValue.DoesNotExist:
                # otherwise, create a new one
                fv = field.fieldvalue_set.create(ad=self.instance)

            # XXX some ugly price fixing
            if field.name.endswith( price ):
                m = re.match( ^$?(d{1,3},?(d{3},?)*d{3}(.d{0,2})?|d{1,3}(.d{0,2})?|.d{1,2}?)$ , value)
                value = m.group(1)
                value.replace( , ,   )
                value =  %.2f  % float(value)

            fv.value = value
            fv.save()

    return self.instance

如何在以上 < code> AdForm 中显示所有创建的字段?

任何赞赏的例子。谢谢。

最佳回答

不需要输入模型格式, 因为 AdForm 类的目的是允许访问这些字段。 为了显示数据库中某个类别中添加的自定义字段, 所需要的只是将字段添加到相应类别的模板中( 假设您使用了应用程序中提供的观点) 。 在分类应用程序中的“ 基” 类模板中, 有这方面的实例 。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签