Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django-如何使用用户访问不同的类?_Django_Django Forms_Django Orm - Fatal编程技术网

django-如何使用用户访问不同的类?

django-如何使用用户访问不同的类?,django,django-forms,django-orm,Django,Django Forms,Django Orm,这是my models.py: class Image(models.Model): user = models.ForeignKey(User) caption = models.CharField(max_length=300) image = models.ImageField(upload_to=get_upload_file_name) pub_date = models.DateTimeField(default=datetime.now)

这是my models.py:

class Image(models.Model):
    user = models.ForeignKey(User)
    caption = models.CharField(max_length=300)
    image = models.ImageField(upload_to=get_upload_file_name)
    pub_date = models.DateTimeField(default=datetime.now)

    class Meta:
        ordering = ['-pub_date']
        verbose_name_plural = ('Images')

    def __unicode__(self):
        return "%s - %s" % (self.caption, self.user.username)


class ProfilePic(Image):
    pass



class BackgroundPic(Image):
    pass


class Album(models.Model):
    name = models.CharField(max_length=150)

    def __unicode__(self):
        return self.name


class Photo(Image):
    album = models.ForeignKey(Album, default=3)
这是另一个:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    permanent_address = models.TextField()
    temporary_address = models.TextField()
    profile_pic = models.ForeignKey(ProfilePic)
    background_pic = models.ForeignKey(BackgroundPic)

    def __unicode__(self):
        return self.user.username
我可以使用其用户对象访问父类

>>>m = User.objects.get(username='mika')
>>>m.image_set.all()
>>>[<Image: mika_photo - mika>, <Image: mika_pro - mika>, <Image: mika_bf - mika>]

还有这个

编辑

>>>m.profilepic_set.all() 
AttributeError:'User' object has no attribute 'profilepic_set'
但都给了我错误

编辑

>>>m.profilepic_set.all() 
AttributeError:'User' object has no attribute 'profilepic_set'
如何访问子类,以便将图像从一个类添加到另一个类

例如:将图片从照片复制到ProfilePic,或从ProfilePic复制到背景图片等等。或者简单地说,如何为特定类中的特定用户添加图像

编辑

>>>m.profilepic_set.all() 
AttributeError:'User' object has no attribute 'profilepic_set'

我想要的是,每个用户将有一组
个人资料图片
背景图片
和其他一组上传的
照片
。这些图像将分别保存在模板中。如果用户愿意,他可以轻松地使用(复制)上传的另一组照片或来自背景图像集的图像作为配置文件图片,并且该图像将添加到
配置文件图片集
。或者,如果他愿意,他可以使用
个人资料图片集
或其他
上传照片
中的图像作为
背景图像
,类似地,从其他类使用的图像将复制到
背景图像集


请指导我实现上述目标。非常感谢。谢谢。

一个简单的方法就是直接对继承的模型进行筛选:

ProfilePic.objects.filter(user=m)
我无法立即回忆起您使用的相关
\u set
语法是否适用于任何形式的继承模型

UserProfile
模型上,您还为
profile\u pic
background\u pic
定义了外键-如果您试图访问这些图像,外键正常工作:

m.profile_pic

我不知道为什么您既有外键,又有一个不起任何作用的专用子类。

首先,请注意自Django 1.5以来发生了变化的用户模型:您现在可以提供一个用户模型,而无需链接到Django“User”类

然后,您可以做两件事:

(正如彼得解释的那样)

当您不想将pic链接到用户时(而是将用户链接到pic)

如果每个用户可以有许多图片,并且每个图片可以有许多用户,那么您需要的是“多对多字段”

如果您想要用户的化身,请在“用户”对象中使用图片。 如果您想为用户提供多张图片(如“相册”中的图片),请使用ForeignKey创建图片模型。 如果您想将多张图片链接到多个用户(如“标记为收藏夹”),请使用多对多

然后,要将一个文件复制到另一个文件,请使用以下方法:

from PIL import Image
class MyImages(models.Model):
      photo_one = models.ImageField(upload_to="one/", blank=True, null=True)
      photo_two = models.ImageField(upload_to="two/", blank=True, null=True)
      def copy(self):
           self.photo_two.save(self.photo_one.name, ContentFile(file(self.photo_one.path).read()))

他们给了你什么错误?另外,你的类模型定义真的通过了吗?@karthikr请查看编辑。是的,他们都有通行证。我是这里的新手,如果我错了,请纠正我。我该如何实现上述目标呢?通常情况下,让模型使用来自非抽象父级的多表继承,但本身没有特殊行为,这是一种很好的设计。你想用这个设计做什么?@PeterDeGlopper假设,我上传了一张图片。然后,我应该能够在用户的UserProfile中将其用作profile\u pic/background\u pic,这样我就可以在模板中将其用作profile\u pic/background image。而且,如果必须的话,我可以很容易地使用
背景图片
并将其用于
个人资料图片
,方法是将我想从
背景图片
添加到
个人资料图片
,或者相反。我希望我是清楚的。请问我,不是这样的。我想你把事情弄得比我需要的更复杂了。因此,我不明白为什么您需要的不仅仅是
图像
模型,然后是
用户配置文件
模型上的外键。是的,谢谢。但是假设,如果我不得不这样做,我如何将其他类中的更多图像附加或添加到
ProfilePic
BackgroundPic
?我对您试图做的事情有点困惑。您可以直接设置用户配置文件的
profile\u pic
属性:
m.profile\u pic=some\u image\u instance
。我想要的是,每个用户都有一组
配置文件图片
背景图片
和其他上传的
照片。这些图像将分别保存在模板中。如果用户愿意,他可以轻松地使用(复制)上传的另一组照片或来自背景图像集的图像作为配置文件图片,并且该图像将添加到
配置文件图片集
。或者,如果他愿意,他可以使用
个人资料图片集
或其他
上传照片
中的图像作为
背景图像
,类似地,从其他类使用的图像将复制到
背景图像集
。正如我所说,在这种情况下,多对多。
class User (models.Model):
    pic = models.ForeignKey(Pic)

class Pic (models.Model):
    picture = models.ImageField(upload_to=get_upload_file_name)

u = User.objects.get(pk=42)
u.pic # --> gives you the one picture that a user can have

p = Pic.objects.get(pk=42)
p.user_set # --> gives you 0 to (many) users that have this pic linked to it.
from PIL import Image
class MyImages(models.Model):
      photo_one = models.ImageField(upload_to="one/", blank=True, null=True)
      photo_two = models.ImageField(upload_to="two/", blank=True, null=True)
      def copy(self):
           self.photo_two.save(self.photo_one.name, ContentFile(file(self.photo_one.path).read()))