Djangoのコメント:モデルを拡張していない、ユーザーのURLを削除したいです。方法?

StackOverflow https://stackoverflow.com/questions/1456267

質問

私は完全にジャンゴにコメントアプリの拡大にドキュメントを理解していて、本当に自動機能に固執したいと思います。のが、 ...

現在のアプリケーションでは、私はコメントと一緒に提出する「URL」のために全く使用していません。

デフォルト設定ののの最小侵襲的なので、の方法を私はコメントフォームので現れてからこのフィールドを防ぐことができますか?

Djangoの1、またはトランク、そしてできるだけ多くのジェネリック/ビルトインを使用する(一般的な見解を、デフォルトのコメントは、設定など、私持っている唯一の単一汎用ビューラッパー今のところ)。

他のヒント

私はいくつかの理由でSmileyChris'ポストにコメントすることはできませんので、私はそれをここに投稿するつもりです。しかし、私はちょうどSmileyChrisの応答を使用してエラーに走りました。またCommentFormを削除したものをポストキーを探すために起こっているので、get_comment_create_data機能を上書きする必要があります。私は3つのフィールドが削除した後、だからここに私のコードです。

class SlimCommentForm(CommentForm):
"""
A comment form which matches the default djanago.contrib.comments one, but with 3 removed fields
"""
def get_comment_create_data(self):
    # Use the data of the superclass, and remove extra fields
    return dict(
        content_type = ContentType.objects.get_for_model(self.target_object),
        object_pk    = force_unicode(self.target_object._get_pk_val()),
        comment      = self.cleaned_data["comment"],
        submit_date  = datetime.datetime.now(),
        site_id      = settings.SITE_ID,
        is_public    = True,
        is_removed   = False,
    )


SlimCommentForm.base_fields.pop('url')
SlimCommentForm.base_fields.pop('email')
SlimCommentForm.base_fields.pop('name')

これは、あなたが上書きされている機能です。

def get_comment_create_data(self):
    """
    Returns the dict of data to be used to create a comment. Subclasses in
    custom comment apps that override get_comment_model can override this
    method to add extra fields onto a custom comment model.
    """
    return dict(
        content_type = ContentType.objects.get_for_model(self.target_object),
        object_pk    = force_unicode(self.target_object._get_pk_val()),
        user_name    = self.cleaned_data["name"],
        user_email   = self.cleaned_data["email"],
        user_url     = self.cleaned_data["url"],
        comment      = self.cleaned_data["comment"],
        submit_date  = datetime.datetime.now(),
        site_id      = settings.SITE_ID,
        is_public    = True,
        is_removed   = False,
    )

私の迅速かつ汚いソリューション:私は「このフィールドは必須です」のエラーを取り除くために任意の値で、「電子メール」と「URL」フィールドに隠されたフィールドを作った。

これはエレガントではないのですが、それは迅速だと私はCommentFormをサブクラス化する必要はありませんでした。コメントを追加するすべての作業はいいです、テンプレート、で行われました。それはこのようになります(警告:テストされていない、それは私の実際のコードの簡易版だから):

{% get_comment_form for entry as form %}

<form action="{% comment_form_target %}" method="post"> {% csrf_token %}

{% for field in form %}

    {% if field.name != 'email' and field.name != 'url' %}
        <p> {{field.label}} {{field}} </p>
    {% endif %}

{% endfor %}

    <input type="hidden" name="email" value="foo@foo.foo" />
    <input type="hidden" name="url" value="http://www.foofoo.com" />

    <input type="hidden" name="next" value='{{BASE_URL}}thanks_for_your_comment/' />
    <input type="submit" name="post" class="submit-post" value="Post">
</form>
scroll top