如何在Django中使用外键?

时间:2012-01-31 18:03:47

标签: django django-models django-forms django-views

我在django有一个视频模型。目前,当保存视频时,当前记录的用户不会保存在模型中(其他字段具有值,但用户字段除外)。该表单使用通用的create django视图。 我想知道如何在保存新视频时保存当前登录的用户?

多媒体/ models.py

from django.db import models
from django.contrib.auth.models import User
from django.forms import ModelForm
from django.core.exceptions import PermissionDenied
from django.db.models.signals import post_save
from django.contrib.auth.decorators import login_required


class Video(models.Model):
    user = models.ForeignKey(User, related_name='+',blank=True, null=True)
    title = models.CharField(max_length=200)
    description = models.TextField()
    created = models.DateTimeField('date created', auto_now_add=True, blank=False)
    file_url = models.CharField(max_length=2000, blank=True)
    file_name = models.CharField(max_length=255, blank=True)
    file_uploaded = models.DateTimeField('date file uploaded', null=True, blank=True)
    file_upload_speed = models.FloatField(null=True, blank=True)
    file_size = models.IntegerField(null=True, blank=True)

    def has_file(self):
        return len(self.file_url) > 0

    def __unicode__(self):
        return self.title

    @models.permalink
    def get_absolute_url(self):
        return ('multimedia_video_detail', [self.id])

class VideoForm(ModelForm):
    class Meta:
        model = Video
        # fields = ('title', 'description')
        exclude = ('file_url', 'file_name', 'file_uploaded','file_upload_speed', 'file_size')

多媒体/ views.py

from django.conf.urls.defaults import patterns, include, url
from models import Video, VideoForm
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$','django.views.generic.list_detail.object_list',{ 'queryset': Video.objects.all() }, name='multimedia_video_list'),    
    url(r'^new$', 'django.views.generic.create_update.create_object',{ 'model': Video },name='multimedia_video_new'),
    url(r'^(?P<object_id>[\d]*)$', 'django.views.generic.list_detail.object_detail',{ 'queryset': Video.objects.all() }, name='multimedia_video_detail'),
    url(r'^(?P<object_id>[\d]*)/edit$','django.views.generic.create_update.update_object',{ 'form_class': VideoForm }, name='multimedia_video_edit'),
    url(r'(?P<object_id>[\d]*)/delete$', 'django.views.generic.create_update.delete_object', { 'model': Video, 'post_delete_redirect': '/videos' }, name='multimedia_video_delete'),    
    url(r'^(?P<object_id>[\d]*)/upload$','multimedia.views.upload_video',name='multimedia_video_upload'),
    url(r'^(?P<object_id>[\d]*)/upload/done$','multimedia.views.upload_video_done',name='multimedia_video_upload_done'),
)

感谢您解决此问题的任何帮助

1 个答案:

答案 0 :(得分:1)

不是直接使用通用创建视图,而是需要使用它周围的包装器将当前用户拉出请求。此外,您创建一个接受用户的模型表单,并覆盖save()方法以在模型实例上设置用户值。

def VideoForm(user):
    class _wrapped(forms.ModelForm):
        class Meta:
            model = models.Video

        def save(self, *args, **kwargs):
            self.instance.user = user
            super(_wrapped, self).save(*args, **kwargs)

    return _wrapped

@login_required
def create(request):
    return django.views.generic.create_update.create_object(form_class=VideoForm(request.user), name='multimedia_video_new')