Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django flatpages default site

mostly I use flatpages for one site (from sites framework). How can I mark existing site default for all created flatpages? It is waste of time every time to choose same site for every page created. Is there any way to override this in models or save method?

like image 309
Ali Ismayilov Avatar asked Jan 24 '11 20:01

Ali Ismayilov


2 Answers

allow for blank in sites field (models.py -> FlatPage.sites) and put this in your model Form (admin.py -> FlatPageForm)

def clean_sites(self):
    sites = self.cleaned_data.get('sites')
    print sites
    return [Site.objects.get(id=settings.SITE_ID)]

edit: I've come up with a better solution. This makes current site already focused in ManyToMany sites field. Edit flatpages/admin.py. Add :from django.contrib.sites.models import Site in the beggining of file and put following code into class FlatPageAdmin:

    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
    if db_field.name == "sites":
        kwargs["initial"] = [Site.objects.get_current()]
    return super(FlatPageAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

There is no need to allow for blank in sites field in FlatPage model

like image 116
seler Avatar answered Sep 23 '22 15:09

seler


Here is my code, it works:

class CustomFlatPage(FlatPage):
    def save(self):
        super(CustomFlatPage, self).save()
        self.sites = [Site.objects.get(pk=1)]
        super(CustomFlatPage, self).save()
like image 43
Ali Ismayilov Avatar answered Sep 20 '22 15:09

Ali Ismayilov