绕过表单字段django的验证

时间:2016-08-27 20:05:40

标签: python django

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

我试图绕过“视频”所需的字段,但很难这样做。任何建议将不胜感激。

这是我的models.py,希望有助于知道如何继续这样做。

            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)

1 个答案:

答案 0 :(得分:0)

the docs看起来它应该支持从版本0.3以来的空,我建议尝试

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

文档说它应该像URL字段一样运行,所以只需标准符号即可。

祝你好运!