1

Thanks for the time for helping me out.

I'm pretty new to django and python.

So I have a model, that I'm trying to pull some data from another model to build out a landing page of sorts. I was trying to make it completely customizatable in the admin but I discovered for what I wanted do I was going to have to involve AJAX. I've scrapped that, and resorted to mostly removing the admin customization because this is really just for my own personal site.

So to boil down the overall goal:

  • There are many "gallery" pages I want to pull the first image from

  • Each gallery on my "landing" page will be a single image from each gallery, title, and url to the gallery.

This is part of my model: class AggeragateImages(Orderable):

    aggeragate = models.ForeignKey("AggeragatePage", related_name="thumbnails")
    gallery_titles = models.CharField(editable=False, max_length=1000)
    gallery_slug = models.CharField(editable=False, max_length=1000)

    def getGallery():
        """
        Returns PK of all Gallery content type pages
        """
        galleryPK = []

        for e in Page.objects.filter(content_model='gallery'):
            galleryPK.append(e.pk)

        return galleryPK

    galleryPK = getGallery()

    for e in galleryPK:
        gallery_titles = Gallery.objects.get(pk=e).titles
        gallery_titles.save()
        gallery_slug = Gallery.objects.get(pk=e).slug
        gallery_slug.save()

    def __unicode__(self):
        return self.name

However why I run a syncdb I get: AttributeError: 'unicode' object has no attribute 'save'

I've also trying doing this through the interactive shell to, and I get the same error when calling 'save()'

Am I really far off base? I really appreciate your help.

1 Answer 1

2

The problem is at:

gallery_titles = Gallery.objects.get(pk=e).titles
gallery_titles.save()

When you do Gallery.objects.get(pk=e), it returns you a model instance, however then you retrieve it's titles attribute which I guess is a string (unicode). So at that point, gallery_titles is a sting which you try to save on the next line, but unicode class does not have a method save which causes an error.

As a side note, it's probably not the best idea to put logic code directly inside a class definition. You can factor your logic into class methods which would be much more appropriate. When you call a class method inside it's definition you are still defining a class attribute.

Sign up to request clarification or add additional context in comments.

2 Comments

do you have a suggested route I should take with saving the unicode as an object?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.