0

I'm new to Django and I've encountered some problem: I'm trying to prepare website where users can attach themselves to custom model "UserGroup" (not these from User.group) so they'll become it's members as ManyToManyField relation. But I would like also to add information to these groups whether they're linked to other groups. How can do such thing? I've tried to do this like here, in UserGroup definition:

related_groups = models.ManyToManyField('UserGroup', blank=True)

But apparently I can't do that this way, I'm getting some problems during migration:

django.core.exceptions.FieldDoesNotExist: UserGroup_related_groups has no field named 'from_usergroup'

I suppose it might be because of faulty way of thinking. Thanks for help in advance! :)

szopenak
  • 11
  • 5
  • Please provide all the code required to reproduce the error and the full error traceback. – Klaus D. Nov 30 '16 at 16:09
  • Please **(a)** show your entire model, not just one field and **(b)** be more specific when describing the problem. *"add information to these groups whether they're linked to other groups"* - what do you mean by this? – yuvi Nov 30 '16 at 16:09
  • @KlausD. Sure, here's the whole model (prototype at least): https://codeshare.io/GLOdpa – szopenak Nov 30 '16 at 18:53
  • @yuvi I thought that would be a nice idea to add field that will allow users to link the groups together - for example Group Musicians in city X are linked to another group Drummers from X - each related group would be shown on particular Group view by reading field related_groups :) – szopenak Nov 30 '16 at 18:58
  • The Django docs for M2M fields seem to suggest using `models.ManyToManyField("self")`. Can you try that? – Mad Wombat Nov 30 '16 at 19:16
  • http://stackoverflow.com/questions/11721157/django-many-to-many-m2m-relation-to-same-model – Mad Wombat Nov 30 '16 at 19:17

2 Answers2

0
  1. You are using wrong name of class for user groups. The correct name is Group
  2. You should use OneToOne Field.

So you should write something like:

related_groups = models.OneToOneField('Group', blank=True)

And don't forget to import Group model before!

from django.contrib.auth.models import Group

P.S. There are 2 ways to do this: 1) To make your own class (i.e ExtGroup) and make OneToOneField to original Group Class. You probably were trying to go this way. 2) Use your own user class and group class.

Here is really nice documentation about each way: https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#extending-the-existing-user-model

And here is answer for the same question: Customize Django Group Model

Hope it helps ;)

Community
  • 1
  • 1
yestema
  • 6,932
  • 4
  • 23
  • 29
  • #yestema Yea, i know about built-in Group, but i don't know if my situation would be a good usage for that :) I think, that's for small variety like moderators, admins, pro-users etc. I want to create many, customized groups as a independent models. The problem on which I'm stuck currently is how to link related models together. I posted above whole model, if you still want to help, please take a look over there :) – szopenak Nov 30 '16 at 19:05
0

Use "self" to indicate that you are linking to the same class.

related_groups = models.ManyToManyField("self", blank=True)

As suggested in this SO post Django Many-to-Many (m2m) Relation to same model

Community
  • 1
  • 1
Mad Wombat
  • 14,490
  • 14
  • 73
  • 109
  • thanks! That's the situation I'm in. But still **I get error messages** ... I've switched the line to `related_groups = models.ManyToManyField("self", blank=True)` Here's the error code, just after typing migrations : https://codeshare.io/5O6gx2 – szopenak Nov 30 '16 at 20:23
  • What Django version are you on? – Mad Wombat Nov 30 '16 at 20:59
  • I've deleted db and each migration file, tried to initialize in once more - migrate ran smooth, but during adding UserGroup from admin panel it crashed again... https://codeshare.io/2jPnPa I'm using django 1.10.3 version. – szopenak Nov 30 '16 at 21:07
  • And the DB is default one - sqlite3 – szopenak Nov 30 '16 at 21:11
  • And that's the weird thing! I added `related_users = models.ManyToManyField("self", blank=True)` to the UserProfile **and it works!** But still, the UserGroup crashes adding new instance process... – szopenak Nov 30 '16 at 21:38
  • Finally it worked, after pulling remote repository and starting whole migrations from scratch another time - no idea why, but if someone's have such problem, I'm suggesting removing DB and migration files. – szopenak Dec 01 '16 at 10:09
  • Migrations in Django are pretty fragile, it is easy to screw things up in non-trivial ways and if you are not very familiar with them it can be very hard to figure out what is going on. Back up your dev DB before every migration, so you can test migrations repeatedly and/or restore to whatever version you want. – Mad Wombat Dec 01 '16 at 16:28