views:

691

answers:

1

i'm writing a data migration using south... but the question refers to the use of select_related to retrieve many to many fields.

the documentation specifies that select_related can be used for fields with relationships using ForeignKey ... i can't fathom it wont' work with ManyToMany fields right?

my field have blank=null but the documentation says you can still call select_related('field_name') and it will pull the relevant relationships. yet when i'm trying to do this below...

where listing is a item in a queryset

for listing in RealEstateListing.objects.all():
        listing_type_slug_url = slugify(listing.listing_type.name)
        sub_type = orm.SubType.objects.get(slug_url=listing_type_slug_url)
        pricing_option = PricingOption.objects.get(name=listing.pricing_option.name)
        lt = orm.Listing(listing_type=sub_type.parent,
                         sub_type=sub_type,
                         expiration_date=listing.expiration_date,
                         title=listing.title,
                         slug_url = listing.slug_url,
                         description = listing.description,
                         contact_person=listing.contact_person,
                         secondary_contact=listing.secondary_contact,
                         address=listing.address,
                         location=listing.location,
                         price=listing.price,
                         pricing_option=pricing_option,
                         display_picture=listing.display_picture,
                         image_gallery=listing.image_gallery,
                         date_added=listing.date_added,
                         status=listing.status,
                         featured_on_homepage=listing.featured_on_homepage,
                         )
        lt.save()    

        lt.features.clear()
        if listing.property_features:
            property_features = listing.property_features.all()
        else:
            property_features = None
        if property_features:    
            for ft in property_features:
                ft_ = Feature.objects.get(name=ft.name)
                lt.features.add(ft_)

i get an error telling me that it cannot resolve the field property_features ... the available fields are only id & name... it seems its not pulling the relationships.

my other question is if we can't use select_related to access many to many fields what is the alternative?

------------------------------------------------EDIT----------------------------------------

i removed the reference to south's fake orm which i was using to do the data migration.

what i'm basically doing with the code above is taking all RealEstateListings objects then in a for loop creating a new Listing object with the data from the old RealEstateListings

the last part is where i have the problem and gives me the error above with the many to many field property_features from the old RealEstateListing model

+1  A: 

It's prettymuch impossible to debug your code as it seems to be failing in a method your using. My guess is that you are doing objects.filter(property_features=...) hoping to replace property_features with a value. You can't do that since it's written as a keyword argument.

It also seems like you have misunderstood the use of select_related. It is purely used to avoid extra queries accessing related objects. I'm not sure about M2M fields but they are not the same as FK, so don't expect them to react the same.

Edit:

So RealEstateListing have a M2M called property_features

First off this if statement is no good:

if listing.property_features: 
    property_features = listing.property_features.all()
else:
    property_features = None
if property_features:

Since all RealEstateListing has a M2M this will always be true as isting.property_features would return a django.db.models.fields.related.ManyRelatedManager object which will evaluate to True. If there wasn't a M2M field on the listing you would get an AttributeError instead. This is not the error you are getting, however, but you should change your above code to:

property_features = listing.property_features.all()
if property_features:

This will do the same since if there are no property_features, on the object the property_features variable will be an empty list.

Now to the actual error. Again I must say that nothing you have written here can produce that error. Like before you would get such an error if you did something like

Listing.objects.filter(property_features=x)

Posting the full tradeback would help identify where problem arise.

googletorp
hi, see my edit above, i removed reference to south's fake orm. object
Rasiel
thanks this has me stumped also as nothing seems to be wrong and no, i'm not doing your latter codes sample. i will post the traceback later on.
Rasiel