2012-11-12 15 views
12

Ho un campo m2m chiamato "admins" all'interno di un modello e ho bisogno di ottenere da una vista tutte le voci selezionate in quel campo, che sono ID utente. Quindi con gli ID utente ottenere le e-mail per ogni utente. È possibile?Django - recupera oggetti da un campo molti a molti

L'esatta cosa che voglio fare è inviare un monumento a tutti gli amministratori di uno spazio all'interno di questa piattaforma.

gli ambienti, il modello:

class Space(models.Model): 

    """  
    Spaces model. This model stores a "space" or "place" also known as a 
    participative process in reality. Every place has a minimum set of 
    settings for customization. 

    There are three main permission roles in every space: administrator 
    (admins), moderators (mods) and regular users (users). 
    """ 
    name = models.CharField(_('Name'), max_length=250, unique=True, 
     help_text=_('Max: 250 characters')) 
    url = models.CharField(_('URL'), max_length=100, unique=True, 
     validators=[RegexValidator(regex='^[a-z0-9_]+$', 
     message='Invalid characters in the space URL.')], 
     help_text=_('Valid characters are lowercase, digits and \ 
    admins = models.ManyToManyField(User, related_name="space_admins", verbose_name=_('Administrators'), help_text=_('Please select the \ 
     users that will be administrators of this space'), blank=True, 
     null=True) 
    mods = models.ManyToManyField(User, related_name="space_mods", 
     verbose_name=_('Moderators'), help_text=_('Please select the users \ 
     that will be moderators of this space.'), blank=True, null=True) 
    users = models.ManyToManyField(User, related_name="space_users", verbose_name=_('Users'), help_text=_('Please select the users that \ 
     can participate on this space'), blank=True, null=True) 

View per l'invio di una sola e-mail:

@login_required 
def add_intent(request, space_url): 

    """ 
    Returns a page where the logged in user can click on a "I want to 
    participate" button, which after sends an email to the administrator of 
    the space with a link to approve the user to use the space. 

    :attributes: space, intent, token 
    :rtype: Multiple entity objects. 
    :context: space_url, heading 
    """ 
    space = get_object_or_404(Space, url=space_url) 
    #admins = space.admins?? 

    try: 
     intent = Intent.objects.get(user=request.user, space=space) 
     heading = _("Access has been already authorized") 

    except Intent.DoesNotExist: 
     token = hashlib.md5("%s%s%s" % (request.user, space, 
          datetime.datetime.now())).hexdigest() 
     intent = Intent(user=request.user, space=space, token=token) 
     intent.save() 
     subject = _("New participation request") 
     body = _("User {0} wants to participate in space {1}.\n \ 
       Please click on the link below to approve.\n {2}"\ 
       .format(request.user.username, space.name, 
       intent.get_approve_url())) 
     heading = _("Your request is being processed.") 
     send_mail(subject=subject, message=body, 
        from_email="[email protected]", 
        recipient_list=[space.author.email]) 

     # Send a notification to all the admins in that space 
     #send_mass_mail() 

    return render_to_response('space_intent.html', \ 
      {'space_name': space.name, 'heading': heading}, \ 
      context_instance=RequestContext(request)) 
+0

Si prega di inserire il codice pertinente del modello. – danihp

+0

@danihp Siamo spiacenti, hai ragione. Ho aggiunto il modello dati e la vista corrente –

risposta

21

è possibile:

space.related.all() per restituire un QuerySet di utente

+1

Restituisce un oggetto Spazio AttributoError non ha "related_admins" http://imgur.com/nEMQi –

+10

Non è necessario utilizzare il relativo nome lì. Semplicemente 'lo farà space.admins.all()'. –

+0

Cosa succede quando si vuole 'space.admins.all()' restituisce 'RecursionError'? – alvas

7

per Django 1.9

space.admins.all() 

* Come menzionato da Salvatore Iovene

Problemi correlati