将用户配置文件保存到模型

时间:2014-02-23 22:51:41

标签: django django-class-based-views user-profile

当表单保存时,我想通过引用用户的“个人资料”页面来保存对象。因此,每个配置文件都可以列出用户提出的每个要约, 见Class based views query: get objects referenced by another model

ofertoj_oferto.profile_id may not be NULL

我的“Oferto”模型包含用户个人资料的ForeignKey。现在我不知道如何告诉系统“获取登录用户的个人资料并使用该ID保存此Ofeto”

也许有一些方法可以查找与用户关联的配置文件,并且不需要Oferto模型上的用户和配置文件。

但我仍然需要一份由用户在该档案中制作的每一份清单

profiles.models

class Profile(BaseInfo):
    bio = models.TextField(max_length=15000000)
    user = models.ForeignKey(User)

views.py

class OfertoCreateView(LoginRequiredMixin, Turtle_CreateView):
    model = Oferto
    action = "created"
    form_class = OfertoCreateForm

forms.py

class OfertoCreateForm(Turtle_Form):
    class Meta:
        model = Oferto
        fields = ("name",
                    "description",
                    "tags",
                    "time",
                    "requirements",
                    "location",
                    "image",)

models.py

class Oferto(models.Model):
    user = models.ForeignKey(User)
    profile = models.ForeignKey(Profile)

    name = models.CharField(max_length=150)
    description = models.TextField(max_length=3000)
    time = models.DecimalField(max_digits=10000000, decimal_places=2, null=True)
    stelo = models.DecimalField(max_digits=10000000, decimal_places=2, null=True)

    location = models.TextField(max_length=3000)

    slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('name',))
    tags = tagging.fields.TagField()

    image = models.ImageField(upload_to='Ofertoj', blank=True, null=True)

    requirements = models.TextField(max_length=550000, blank=True, null=True)

    def get_absolute_url(self):
        return reverse('oferto_detail', kwargs={'slug': self.slug})

    def __unicode__(self):
        return self.name

    def get_tags(self):
        return Tag.objects.get_for_object(self)

1 个答案:

答案 0 :(得分:0)

在您的视图/表单/模型保存区域中:调用request.user会返回一个User对象,您可以将其发送到模型的ForeignKey字段