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?
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
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With