willhardy / django-seo

Provides a set of tools for managing Search Engine Optimisation (SEO) for Django sites.
BSD 3-Clause "New" or "Revised" License
251 stars 117 forks source link

Give `populate_from` callables access to model instances #6

Closed willhardy closed 13 years ago

willhardy commented 13 years ago

It's useful to have access to the model instance when providing a default value in a populate_from function.

The documentation for this should point out that it's often better to put this logic in the database, using model class linked metadata and template variables.

I imagine it would be best to require all populate_from functions to accept any arguments provided, by using the **kwargs parameter. The system can then provide model_instance, view_name, model_class or whatever else the relevant backend has available. This will help futureproof implementations using the seo library.

willhardy commented 13 years ago

Closed by bf1972bfe3256a6c5ef0bc79b23d1e73b9dda478. Model instance available to populate_from methods.

blite commented 13 years ago

this doesn't seem to be fully fixed for me

on the example http://django-seo.readthedocs.org/en/latest/reference/definition.html#all-fields

def default_title(metadata, model_instance=None, **kwargs): if model_instance: return "My Website: %s" % model_instance.name else: return None

doesn't work for me, but

def default_title(metadata, model_instance=None, **kwargs):
    if model_instance:
        return "My Website: %s" % model_instance._content_object.name
    else:
        return None  

does work

i'm guessing the error is in this:

        def _process_context(self, context):
            context['content_type'] = self._content_type
            context['model_instance'] = self