3

I have a model that stores URLs scraped from our group chat rooms at the office, I'd like to be able to move an object to an inherited model based on the url. I know inherited fields are simply a OneToOne relation, is there a simple way of of converting the object to inherited model? Here are the models in question:

class Link(models.Model):
    url = models.URLField(max_length=2048, unique=True)
    message = models.TextField()
    room = models.ForeignKey(Room)
    posted = models.DateTimeField()

    def save(self, *args, **kwargs):
        image_types = ['gif', 'jpg', 'jpeg', 'ico', 'png'. 'tiff', 'bmp']
        if self.url.split('.')[-1] in image_types:
             convert to ImageLink
        super(Link, self).save(*args, **kwargs)

class ImageLink(Link):
    pass
Chris Montanaro
  • 16,948
  • 4
  • 20
  • 29
  • what do you mean by 'move an object to an inherited model'? – Mihai Zamfir May 22 '14 at 15:15
  • @MihaiZamfir I have the model ImageLink that is inherited from Link. If I have an object saved as a Link and fits certain criteria I would like to save it instead as an ImageLink, or have the ability to retroactively look at my links table and convert Link objects to ImageLink objects based on said criteria – Chris Montanaro May 22 '14 at 15:20
  • you can get the object from the Link model, then save it to ImageLink, something like `il=ImageLink(url=,message=,...); il.save()`. But why aren't you saving it since the begin as an ImageLink, instead of first saving it as a parent Model? – Mihai Zamfir May 22 '14 at 15:24
  • I want a more generic way of adding a Link, also the ability of converting a Link to an inherited model would give me the ability to add additional inherited models in the future to match on other criteria – Chris Montanaro May 22 '14 at 15:32
  • even if you do so and convert it, you will still have to save it into your `ImageLink` table. (conversion doesn't mean save). But I repeat, I think the entire logic is wrong here... save it into your ImageLink since the begining, instead into the parent class – Mihai Zamfir May 22 '14 at 15:37
  • @MihaiZamfir that doesn't work for existing links that I'd like converted, I don't want to delete an existing object in order to create a OneToOne relationship. What I want is to generically save a Link and add the corresponding OneToOne relationship to effectively change the object to the inherited type. – Chris Montanaro May 22 '14 at 15:42
  • Possible duplicate of [Move a python / django object from a parent model to a child (subclass)](http://stackoverflow.com/questions/4011220/move-a-python-django-object-from-a-parent-model-to-a-child-subclass) – eneepo Nov 16 '16 at 07:49

1 Answers1

0
image_link = models.ImageLink.objects.create(url='http://example.com')
print(image_link.link_ptr.url) #http://example.com
#When we create image_link instance we create image instance implicitly 

link = models.Link.objects.create(url='http://example1.com')
image_link = models.ImageLink.objects.create(link_ptr=link, url=link.url)
"""
To create image_link from link we can use such approach. It won't actually convert link into image_link
since it's impossible for image_link to exist without link.
"""
print(image_link.url) #http://example1.com
Alex Polekha
  • 1,590
  • 15
  • 13