On Tue, Jul 12, 2016 at 2:42 PM, Fellipe Henrique <felli...@gmail.com>
wrote:


> On Tue, Jul 12, 2016 at 1:52 PM, Ramiro Morales <cra...@gmail.com> wrote:
>
>> Where the 'Conjunto de dados' seen in the screenshot title comes from? Is
>> the code you posted actually in sync with it?
>
>
> Sorry about that, I take the screenshot before change these text to
> "DataSet"...
>
> [image: Inline image 1]
>
> My problem is, the second "Dataset" (on light gray line) and
> "Dataset_users object"* <https://github.com/fellipeh>*
> <https://groups.google.com/group/django-users>
> <https://groups.google.com/d/msgid/django-users/CAF1jwZF2WwricBicEKxPL7eWsp%2BQrTuD7YgCQueNfKAWzyTDrA%40mail.gmail.com?utm_medium=email&utm_source=footer>



I've prepared this simplified app.

Note: All the is completely centered around controlling the labels in the
admin views, this can mean things get completely ugly on ther UI areas.

The Dataset model setup is basically what you posted originally, the Sample
model is very similar to Dataset but the M2M relationship between User and
Sample is defined through an explicit SampleUserRel model. See


https://docs.djangoproject.com/en/1.9/topics/db/models/#intermediary-manytomany


https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.ManyToManyField.through

and


https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#working-with-many-to-many-intermediary-models

sections of the Django documentation.

#--------------------------------------------
# models.py
from __future__ import unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible


@python_2_unicode_compatible
class User(models.Model):
    username = models.CharField(max_length=200, blank=True, null=True)
    email = models.EmailField(blank=False, unique=True)

    def __str__(self):
        return self.email


@python_2_unicode_compatible
class Dataset(models.Model):
    active = models.BooleanField(default=True)
    title = models.CharField(max_length=250)
    users = models.ManyToManyField(User)

    def __str__(self):
        return self.title


@python_2_unicode_compatible
class Sample(models.Model):
    active = models.BooleanField(default=True)
    title = models.CharField(max_length=250)
    users = models.ManyToManyField(User, through='SampleUserRel')

    def __str__(self):
        return self.title


@python_2_unicode_compatible
class SampleUserRel(models.Model):
    user = models.ForeignKey(User)
    sample = models.ForeignKey(Sample, verbose_name='this can be set with
the verbose_name option of the FK field to the remote model in the
intermediate model')

    def __str__(self):
        return "this can be set with the intermediate model's __str__
method"

#--------------------------------------------
# admin.py
from django.contrib import admin

from .models import Dataset, User, Sample, SampleUserRel


class DatasetInline(admin.TabularInline):
    model = Dataset.users.through
    verbose_name = 'this can be set with
{Stacked,Tabular}Inline.verbose_name'
    verbose_name_plural = 'this can be set with
{Stacked,Tabular}.verbose_name_plural'


class SampleUserRelInline(admin.TabularInline):
    model = SampleUserRel
    extra = 1
    verbose_name = "this can be set with intermediate's model verbose_name
and overridden with {Stacked,Tabular}Inline.verbose_name"
    verbose_name_plural = "this can be set with intermediate model's
verbose_name_plural and overridden with
{Stacked,Tabular}Inline.verbose_name_plural"


class UserAdmin(admin.ModelAdmin):
    list_display = ('email',)
    inlines = [DatasetInline, SampleUserRelInline]


admin.site.register(User, UserAdmin)
admin.site.register([Dataset, Sample])

Btw, something like this is what we talk about when we ask for an excerpt
of the code with only the relevant parts, please take the time to prepare
things by stripping code unrelated to the issue at hand before posting your
code. Help us to help you.

The above setup results in the following Change User view:

[image: Inline image 1]

Hoping this is of help,

-- 
Ramiro Morales
@ramiromorales

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAO7PdF_dupadcYtQpwWaCOBxTZ13G-rHh%2B4u2sQLbMwa0UJwnA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to