How to copy an object from one model to another model - django

Each user has many albums, and each album has its many photos. And each user has one set background image to hold its many images. Similarly, a user has one set of profile picture to hold its many images.
These are my models:
class UserProfile(models.Model):
user = models.OneToOneField(User)
permanent_address = models.TextField()
temporary_address = models.TextField()
profile_pic = models.ForeignKey(ProfilePicture)
background_pic = models.ForeignKey(BackgroundImage)
class Album(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)
class Photo(models.Model):
album = models.ForeignKey(Album, default=3)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)
class BackgroundImage(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)
class ProfilePicture(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)
Eg: I tried doing this: to let the user copy one image from ProfilePicture to Background Image, but didn't work:
# Get the user
>>>m = User.objects.get(username='m')
# Get its set of Profile pictures
>>>m_pro_set = m.profilepicture_set.all()
>>>m_pro_set
[<ProfilePicture: pro_mik>]
# Get its set of Background images
>>>m_back_set = m.backgroundimage_set.all()
>>>m_back_set
[<BackgroundImage: bg_mik>]
# Get an image object from Profile picture of the user
>>>m_pro_1 = m.profilepicture_set.get(id=2)
>>>m_pro_1
<ProfilePicture: pro_mik>
# Get an image object from Background image of the user
>>>m_back_1 = m.backgroundimage_set.get(id=2)
>>>m_back_1
<BackgroundImage: bg_mik>
# So, I tried to copy one image from BackgroundImage of a user to ProfilePicture
>>>m_pro_set.objects.create(m_back_1)
File "<console>", line 1, object has attribute 'objects'
AttributeError: 'QuerySet' object has no attribute 'objects'
So my question is, how to copy an objects from one model to another? Any advice will be much appreciated.

You'll need to copy the properties of one object to another object. create is used when you want to create a new instance, not when you want to update a specific instance.
m_pro = m_pro_set[0] # m_pro_set is a list and need to get the specific instance
m_pro.image = m_back_1.image
m_pro.caption = m_back_1.caption
m_pro.pub_date = m_back_1.pub_date
m_pro.save()
Alternatively you can create a method on ProfilePicture if this is a common operation to contain this functionality.
class ProfilePicture(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(upload_to=get_upload_file_name)
caption = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=datetime.now)
def update(self, bg_img):
self.image = bg_img.image
self.caption = bg_img.caption
self.pub_date = bg_img.pub_date
self.save()
So you would just need to call m_pro_1.update(m_back_1) if we wanted to make m_pro_1 have the same properties as m_back_1
Also, if you wanted to create a new instance, I'd suggest using a classmethod like the following
class ProfilePicture(models.Model):
...
#classmethod
def create_from_bg(cls, bg_img):
img = cls(user=bg_img.user, image=bg_img.image, caption=bg_img.caption, pub_date=bg_img.pub_date)
img.save()
return img
#classmethod
def create_from_photo(cls, photo):
img = cls(user=photo.album.user, image=photo.image, caption=photo.caption, pub_date=photo.pub_date)
img.save()
return img
This creates a new ProfilePicture and is used: profile_pic = ProfilePicture.create_from_bg(m_back_1)

Related

Django - Linking Models

I am using django and I want to link two models. The first model is comment and the second model is image. I want to have multiple images for one comment and an image should be linked with only one comment.
Comment model has its fields and image model looks like this:
class Image(models.Model):
image = models.ImageField(upload_to=f'{hash(id)}/', null=True, blank=True)
def __str__(self):
return self.image.name
And this is the model that I used to link comment and image:
class CommentImage(models.Model):
comment = models.OneToOneField(Comment, models.CASCADE, null=True)
image = models.ForeignKey(Image, models.CASCADE, null=True)
class Meta:
ordering = ["-id"]
verbose_name = _("Image")
verbose_name_plural = _("Images")
def __str__(self):
return self.image.image.name
Here is the admin panel of django:
enter image description here
As you can see I could be able add only one image and there is no button as well to add multiple image. What should I change to be able to add multiple images?
I have tried using ManytoManyField and removing comment field from CommentImage but it did not work.
I think you are overcomplicating things. Why not just add a text field to your ImageComment:
class Image(models.Model):
def upload_to(self, filename):
return f'{hash(self)}/{filename}'
image = models.ImageField(upload_to=upload_to, null=True, blank=True)
comment = models.ForeignKey(
'ImageComment', on_delete=models.SET_NULL, null=True
)
def __str__(self):
return self.image.name
class CommentImage(models.Model):
comment = models.TextField()
Or in case an Image can have multiple ImageComments as well, use a ManyToManyField:
class Image(models.Model):
def upload_to(self, filename):
return f'{hash(self)}/{filename}'
image = models.ImageField(upload_to=upload_to, null=True, blank=True)
comments = models.ManyToManyField(
'ImageComment'
)
def __str__(self):
return self.image.name
class CommentImage(models.Model):
comment = models.TextField()
You can even add an InlineModelAdmin to make editing comments at the same location as the image possible:
from django.contrib import admin
class ImageCommentInline(admin.TabularInline):
model = ImageComment
#admin.site.register(Image)
class ImageAdmin(admin.ModelAdmin):
inlines = [
ImageCommentInline,
]
You can try using this via manytomanyfields:
class CommentImage(models.Model):
comment = models.ForeignKey(Comment, models.CASCADE)
image = models.ForeignKey(Image, models.CASCADE)
class Comment(models.Model):
# other fields here
images = models.ManyToManyField(Image, through='CommentImage', related_name='comments')

save data to another table after creating django

i have two models ImageShoot and Image.
models.py:
class ImageShoot(models.Model):
name = models.CharField(max_length=100)
# image = models.URLField(name=None)
created_at = models.TimeField(auto_now_add=True)
def __str__(self):
return self.name
class Image(models.Model):
license_type = (
('Royalty-Free','Royalty-Free'),
('Rights-Managed','Rights-Managed')
)
image_number = models.CharField(default=random_image_number,max_length=12)
title = models.CharField(max_length = 100)
image = models.ImageField(upload_to = 'home/tboss/Desktop/image' , default = 'home/tboss/Desktop/image/logo.png')
category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.CASCADE)
shoot = models.ForeignKey(ImageShoot, on_delete=models.CASCADE)
image_keyword = models.TextField(max_length=1000)
credit = models.CharField(max_length=150, null=True)
location = models.CharField(max_length=100, null=True)
license_type = models.CharField(max_length=20,choices=license_type, default='')
uploaded_at = models.TimeField(auto_now_add=True)
def __str__(self):
return self.title
admin.py:
class Imageset(admin.ModelAdmin):
associated_images = ImageShoot.image_set.all()
return associated_images
admin.site.register(Image)
admin.site.register(ImageShoot,Imageset)
what i am trying to achieve that when i create a image it should show on imageshoot also like when i create image in shoot 1. this image should show on shoot 1.
i am not sure which field should i add on imageshoot.
Use Reverse lookup
In Your views you can get all the images associated with the ImageShoot Obj using set.Try this in your shell after briefly going through the docs
associated_images = ImageShoot.image_set.all()
You can also use django orm methods for querysets like filter, count, etc.
Edit:
To display related images you can use this in admin:
#admin.register(ImageShoot)
class Imageset(admin.ModelAdmin):
list_display = ('name', 'created_at', 'associated_images')
def associated_images(self, obj):
return obj.image_set.all() #<----edit
associated_images.admin_order_field = 'imageshoot_image'

Django models issue

I have created a model in Django as shown below
class work(models.Model):
Image = models.FileField()
text = models.CharField(max_length=10000)
sub_text = models.CharField(max_length=10000)
color=models.CharField(max_length=200)
opacity = models.CharField(max_length=200)
link =models.CharField(max_length=10000)
grid_column = models.CharField(max_length=10000,blank=True)
category = models.CharField(max_length=200,default=all)
def __str__(self):
return '{}'.format(self.text)
and while am trying to add values to that models through admin panel it throws the below error
TypeError at /admin/mysite/work_dummy/add/
all() takes exactly one argument (0 given)
Can anyone help me to solve this
max_length for CharField is 255 so whatever you put in Model will not effect that in database, If you want to add more than that use TextField and also default takes a string or int, but since you have use all variable and i cant see a definition of that, i assume that wrong
class work(models.Model):
Image = models.FileField(upload_to='media',blank=True)
text = models.TextField()
sub_text = models.TextField()
color=models.CharField(max_length=200)
opacity = models.CharField(max_length=200)
link =models.TextField()
grid_column = models.TextField(blank=True)
category = models.CharField(max_length=200,default='all')
def __str__(self):
return '{}'.format(self.text)

Selecting a favourite image from a list of associated images in Django Models

I have the following AlbumImage model which is for the images my users upload:
`
class AlbumImage(models.Model):
image = ProcessedImageField(upload_to='albums', processors=[ResizeToFit(1280)], format='JPEG', options={'quality': 70})
alt = models.CharField(max_length=255, default=uuid.uuid4)
created = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(max_length=70, default=uuid.uuid4, editable=False)
`
and the model of people who might appear in the aforementioned pictures:
`
class People(models.Model):
name = models.CharField(max_length=30,default='John Doe')
remind_on = models.DateField()
event = models.TextField(default='No Event')
associated_with = models.ManyToManyField(AlbumImage)
`
Problem: I want my user to be able to select one of the images from the many associated images. How do I represent that in my models?
You can use a OneToOneField as follows,
class People(models.Model):
name = models.CharField(max_length=30,default='John Doe')
remind_on = models.DateField()
event = models.TextField(default='No Event')
associated_with = models.ManyToManyField(AlbumImage)
favourite_image = models.OneToOneField(AlbumImage)

Django 1.5 ModelForm like admin in view with images and foreign key

I have the following models:
class Quiver(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
is_default = models.BooleanField(default=False)
type = models.CharField(max_length=1, choices=QUIVER_TYPES)
category = models.CharField(max_length=255, choices=QUIVER_CATEGORIES)
def __unicode__(self):
return u'[%s] %s %s quiver' % (
self.user.username,
self.get_type_display(),
self.get_category_display())
class Image(models.Model):
photo = models.ImageField(upload_to=get_upload_file_path)
is_cover = models.BooleanField(default=False)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
def save(self, *args, **kwargs):
try:
this = Image.objects.get(pk=self.pk)
if this.photo != self.photo:
this.photo.delete(save=False)
except Image.DoesNotExist:
pass
super(Image, self).save(*args, **kwargs)
class Surfboard(models.Model):
quiver = models.ForeignKey(Quiver)
brand = models.CharField(max_length=255)
model = models.CharField(max_length=255)
length = models.CharField(max_length=255)
width = models.CharField(max_length=255, blank=True)
thickness = models.CharField(max_length=255, blank=True)
volume = models.CharField(max_length=255, blank=True)
images = generic.GenericRelation(Image)
def __unicode__(self):
return u'%s %s %s' % (self.length, self.brand, self.model)
def get_cover_image(self):
"Returns the cover image from the images uploaded or a default one"
for image in self.images.all():
if image.is_cover:
return image
return None
I'd like to be able to have the same form I have in the admin in my frontend view /surfboard/add:
As a new Django fan and user, I started to create the form from scratch. Not being able to do what I want with including the foreign key "quiver" as a dropdown list, I found in the doc the ModelForm, and decided to use it, so here what I got:
class SurfboardForm(ModelForm):
class Meta:
model = Surfboard
In my view, it looks like this and it's already a good start:
So now, I wanted to have a way to add pictures at the same time, and they are linked to a surfboard via a Generic Relation. Here I don't find the way to do a implementation like in the admin, and get frustrated. Any tips to do so?
Thanks!
What you seek is called an inline formset - see the docs for more.
It's also handy that you can render a formset quickly with {{ formset.as_p }}, but you'll need to write some JavaScript (or use the JavaScript that's used in the Django admin) to handle adding and removing forms.