Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Unfortunately there is no way to do this out of the box with Mezzanine. If you look at the BlogPost model class with the <a href="https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/blog/models.py#L11" rel="noreferrer">Mezzanine Blog app</a>, you will see the following:</p> <pre><code>class BlogPost(Displayable, Ownable, RichText, AdminThumbMixin): </code></pre> <p>The subclassing of Ownable turns out to be the important factor here. From line 6 of the models.py file of the Blog app, we know that the Ownable class was imported from Mezzanine's Core app:</p> <pre><code>class Ownable(models.Model): """ Abstract model that provides ownership of an object for a user. """ user = models.ForeignKey(user_model_name, verbose_name=_("Author"), related_name="%(class)ss") class Meta: abstract = True def is_editable(self, request): """ Restrict in-line editing to the objects's owner and superusers. """ return request.user.is_superuser or request.user.id == self.user_id </code></pre> <p>Due to the fact that Ownable defines a ForeignKey relationship, there can be many Ownable objects related to a single User object, but multiple User objects cannot be related to a single Ownable object. Since a BlogPost's author is defined this way, there can only be one author per blog post.</p> <p>In order to allow multiple authors per blog post, you need to create a many-to-many (M2M) field so multiple User objects can be related to a single BlogPost object. To accomplish this, your best bet that doesn't involve changing Mezzanine's source code is to create a custom blog model by subclassing BlogPost:</p> <h2>yourapp/models.py</h2> <pre><code>from django.db import models from mezzanine.utils.models import get_user_model_name from mezzanine.blog.models import BlogPost user_model_name = get_user_model_name() class CollaborativeBlogPost(BlogPost): """ Custom model that subclasses Mezzanine's BlogPost to allow multiple authors """ authors = models.ManyToManyField(user_model_name) def is_editable(self, request): """ Customize is_editable method originally defined in Mezzanine's Ownable class to allow editing by all users """ return request.user.is_superuser or request.user.id in self.authors.all().values_list('id', flat=True) </code></pre> <p>You'll also want to add your new collaborative blog post to your admin. Using some pointers from <a href="http://mezzanine.readthedocs.org/en/latest/model-customization.html" rel="noreferrer">Mezzanine's field injection documentation</a> (which I originally wanted to suggest but there are some issues with it when creating South migrations with ManyToManyFields), you can copy the base BlogPost admin and add the authors field:</p> <h2>yourapp/admin.py</h2> <pre><code>from copy import deepcopy from django.contrib import admin from mezzanine.blog.admin import BlogPostAdmin from mezzanine.blog.models import BlogPost from .models import CollaborativeBlogPost blog_fieldsets = deepcopy(BlogPostAdmin.fieldsets) blog_fieldsets[0][1]["fields"].insert(-2, "authors") class MyBlogPostAdmin(BlogPostAdmin): fieldsets = blog_fieldsets admin.site.register(CollaborativeBlogPost, MyBlogPostAdmin) </code></pre> <p>Depending on your needs you may need to add more admin logic, but hopefully this will get you started.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload