ManyToManyField with rating using 'through' on each ManyToMany relation in Django

181 views
Skip to first unread message

inoyon artlover KLANGRAUSCH

unread,
Dec 3, 2014, 7:16:13 PM12/3/14
to django-d...@googlegroups.com

I am very beginner in the programming world, so please forgive my lack of understanding...

There is a CustomUserprofile in my models.py.. nothin very special about this....

class Interests(models.Model):

    RATING = [(y, y) for y in range(1, 7)]

    interest = models.CharField(max_length=40)
    interest_rating = models.SmallIntegerField(choices=WEIGHT)


class CustomUserprofileInterests(models.Model):

    user = models.OneToOneField(User)
    interests = models.ManyToManyField(
            Interests, through='CustomInterests',
            through_fields=('custominterest', 'interest'),
            null=True, blank=True)


class CustomInterests(models.Model):

    WEIGHT = [(y, y) for y in range(1, 7)]

    interest = models.ForeignKey(Interests)
    custominterest = models.ForeignKey(CustomUserprofileInterests)
    rating = models.SmallIntegerField(choices=WEIGHT)

I want to accomplish a rating on each relation in the 'interests = ManyToManyField' in my CustomUserprofile. The Interests-Model HAS to have an OWN, self relating rating in each 'interest' entry, NOT related to the CustomUserprofile.

Startet lot of investigation and the 'through' option seems to be the solution? Don't got it really because in my Admin there is NO field to choose some 'interests' and an option to rate it.
Got absolutley NO idea how to do it... find NO way out..... would appreciate any hints.

ManyManyMany thanks in advance! cheers...

Yann Fouillat

unread,
Dec 4, 2014, 3:38:33 AM12/4/14
to django-d...@googlegroups.com
Hi,

> Startet lot of investigation and the 'through' option seems to be the
> solution? Don't got it really because in my Admin there is NO field to
> choose some 'interests' and an option to rate it.

Did you try using InlineModelAdmin
(https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#working-with-many-to-many-intermediary-models)?


--
Yann FOUILLAT
Ingénieur Développeur
Makina Corpus
Tél : 02 51 79 80 82

Florian Apolloner

unread,
Dec 4, 2014, 3:49:48 AM12/4/14
to django-d...@googlegroups.com
Hi,

this mailing list is about the development of Django itself. Usage questions should go to django-users.

inoyon artlover KLANGRAUSCH

unread,
Dec 4, 2014, 7:35:39 AM12/4/14
to django-d...@googlegroups.com
Sorry about this...

inoyon artlover KLANGRAUSCH

unread,
Dec 4, 2014, 8:28:17 AM12/4/14
to django-d...@googlegroups.com, y...@makina-corpus.com


Many thanks for the answer! Very Now I made a big step forwards:

class CustomInterestsInline(admin.TabularInline):
    model = models.CustomInterests
    extra = 9

class CustomSportsInlilne(admin.TabularInline):
    model = models.CustomSports
    extra = 9

class CustomMusicInline(admin.TabularInline):
    model = models.CustomMusic
    extra = 9

class CustomUserprofileAdmin(admin.ModelAdmin):
    inlines = (CustomInterestsInline, CustomSportsInlilne, CustomMusicInline,)

The 'extra' attribute defines the amount of rows. The rating option is displayed also.

The nexts steps I want to accomplish:

A form for users, they can create/edit the profile. The 'interests' and 'sports'  columns have to
be prepopulated with all available (declared by 'active' with a Boolean) entries in the interests/sports table.
If a user has an 'interest' or 'sports', he/she selects it (checkbox) and rates the entry.
Also the amount of entries to be selected by the user is limited to a defined amount (i.e. 6 or 10).
Is there any way to do it???


I started with something like this....

class UserProfileForm(forms.ModelForm):

    interests = forms.ModelMultipleChoiceField(
        queryset=Interests.objects.all(),
        widget=forms.CheckboxSelectMultiple)

    sports = forms.ModelMultipleChoiceField(
        queryset=Sports.objects.all(),
        widget=forms.CheckboxSelectMultiple)


Best regards! :)







 Great, so far.
As next step, I want to

inoyon artlover KLANGRAUSCH

unread,
Dec 4, 2014, 8:30:11 AM12/4/14
to django-d...@googlegroups.com, y...@makina-corpus.com
Am Donnerstag, 4. Dezember 2014 09:38:33 UTC+1 schrieb Yann Fouillat:
Reply all
Reply to author
Forward
0 new messages