Hi Joey.

I was trying to understand your models, relationship and logic behind them.

How you are supposed to create a League object? 
I tried creating as follows.
p=Player.objects.create(name='Shahid Afridi')
l=League(name='FootballDevils')
l.save() // not calling directly objects.create on League as you're 
checking some boolean in its save method.

and as i expected, it says that you have to create League object via 
LeaguePlayer intermediary Model, but in LeaguePlayer model you need to pass 
a League object first.

This might clear what i'm trying to say 
https://docs.djangoproject.com/en/1.8/topics/db/models/#extra-fields-on-many-to-many-relationships

I will suggest you to not override save method in League model as you want 
an Intermediary table as well and Django doesn't let you use add method, 
rather use post_save signal on League object save event and attach all 
available player objects to newly created league via LeaguePlayer.

I've created a pastebin here for the same. http://paste.ubuntu.com/12015824/

Zubair Alam


On Thursday, August 6, 2015 at 8:47:09 AM UTC+5:30, JoeLinux wrote:
>
> Dear Anyone,
>
> I'm having an issue with a ManyToMany "through" table not being recognized 
> by the makemigrations command. Please consider the model definitions at the 
> following pastebin: http://pastebin.ubuntu.com/12011412/
>
> So, I have a Player model. Players are stored and updated in that model 
> (also for clarification, the Player model is in a separate app because it's 
> used for other purposes). The League model, when created, makes available 
> all of the Player objects in the DB for drafting to a LeagueTeam. That's 
> why there's a "through" table for LeaguePlayer, since I don't want to 
> affect the original Player object, and I want to add the additional 
> information as to what LeagueTeam the LeaguePlayer got drafted to (think 
> fantasy football).
>
> However, I'm getting errors in the console about the draft_leagueplayer 
> (the relevant app's name is "draft") table not existing, and I noticed it's 
> not getting created in the db. After a little digging, I realized that 
> ultimately the django.apps.config module's AppConfig.get_models() method is 
> set to ignore any apps with "auto_created = True" by default, thus ignoring 
> LeaguePlayer. If you look at the pastebin above, I am adding Player objects 
> to the League as soon as it's created, and the "auto_created = True" meta 
> attribute helps me do that easily.
>
> I'm wondering if there is a way around this. I need extra information in 
> this Many-to-Many relationship (the team the player is drafted to), and I'm 
> surprised the auto_created attribute exists and doesn't seem to work (since 
> the migration ignores the model). Is this a bug, or should I be manually 
> creating LeaguePlayer objects on-the-fly when adding to the League object 
> instead of using auto_created? Am I misusing auto_created?
>  
> Joey "JoeLinux" Espinosa
> about.me/joelinux
> [image: Joey "JoeLinux" Espinosa on about.me]
>   <http://about.me/joelinux>
>
>

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a6cc21bf-b352-4501-9cb7-6db54ac4c1b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to