2016-08-27 5 views
-1
  class PostForm(forms.ModelForm): 
       description = forms.CharField(widget=PagedownWidget(show_preview=False)) 
       class Meta: 
        model = Post 
        fields = [ 
         'title', 
         'image', 
         'video', 
         'description', 
         'public', 
         'tags', 
         ] 

私は 'ビデオ'の必須フィールドをバイパスしようとしていますが、それは難しいです。任意の提案をいただければ幸いです。フォームフィールドdjan​​goの妥当性チェックをバイパスします

これは私のmodels.pyです。うまくいけば、これをどうやって進めるのかを知っておくと助かります。それは、バージョン0.3以降、今空にサポートすべきであるように見えるthe docsから

  from django.db import models 

      from django.db.models import Count, QuerySet, F 
      from django.utils import timezone 
      from django.conf import settings 
      from django.contrib.contenttypes.models import ContentType 
      from django.core.urlresolvers import reverse 
      from django.db.models.signals import pre_save 
      from django.utils.text import slugify 
      from markdown_deux import markdown 
      from django.utils.safestring import mark_safe 
      from embed_video.fields import EmbedVideoField 
      from taggit.managers import TaggableManager 

      from comments.models import Comment 

      def upload_location(instance, filename): 
       return "%s/%s" %(instance.slug, filename) 


      class Post(models.Model): 
       user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1) 
       title = models.CharField(max_length=75) 
       slug = models.SlugField(unique=True) 
       video = EmbedVideoField() 
       image = models.ImageField(
         upload_to=upload_location, 
         null=True, 
         blank=True, 
         width_field="width_field", 
         height_field="height_field") 
       height_field = models.IntegerField(default=0) 
       width_field = models.IntegerField(default=0) 
       description = models.TextField() 
       tags = TaggableManager() 
       public = models.BooleanField(default=False) 
       updated = models.DateTimeField(auto_now_add=False, auto_now=True) 
       created = models.DateTimeField(auto_now_add=True, auto_now=False) 


       def __str__(self): 
        return self.title 

       def get_absolute_url(self): 
        return reverse("posts:detail", kwargs={"slug": self.slug}) 

       class Meta: 
        ordering = ["-created", "-updated" ] 

       def get_markdown(self): 
        description = self.description 
        markdown_text = markdown(description) 
        return mark_safe(markdown_text) 

       @property 
       def comments(self): 
        instance = self 
        qs = Comment.objects.filter_by_instance(instance) 
        return qs 

       @property 
       def get_content_type(self): 
        instance = self 
        content_type = ContentType.objects.get_for_model(instance.__class__) 
        return content_type 


      def create_slug(instance, new_slug=None): 
        slug = slugify(instance.title) 
        if new_slug is not None: 
         slug = new_slug 
        qs = Post.objects.filter(slug=slug).order_by("-id") 
        exists = qs.exists() 
        if exists: 
         new_slug = "%s-%s" %(slug, qs.first().id) 
         return create_slug(instance, new_slug=new_slug) 
        return slug 



      def pre_save_post_receiver(sender, instance, *args, **kwargs): 
       if not instance.slug: 
        instance.slug = create_slug(instance) 


      pre_save.connect(pre_save_post_receiver, sender=Post) 
+0

私は現在django-embed-videoを使用していますが、ビデオのURLを要求しますが、フォームを完了する必要があります。 –

+0

あなたのモデルフィールドはビデオフィールドに '空白'および/または 'null'を許可しますか?モデルのフォームはモデルそのものに設定されている制限に従います – derelict

+0

私は先に進み、models.pyを追加しました。どう考えているか教えてください。 –

答えて

0

、私はドキュメントは、それがURLフィールドのように機能する必要があり、これだけの標準的な表記法があるべきと言う

video = EmbedVideoField(null=True,blank=True) 

を試みることをお勧めあなたが必要とするものすべて。

幸運!

+0

それは働いて、ありがとう!私は最初に必要な=偽を見ていたが、行っていない。再度、感謝します。 –

関連する問題