ManyToMany question
I'm making a game link site, where users can post links to their favorite web game. When people post games they are supposed to check what category the game falls into. I decided to allow many categories for each game since some games can fall into many categories. So the question is, how do I handle this in my view? And how can I show it as Checkboxes, where at least one has to be checked? And how can I show this as checkboxes in the Admin as well? Here is the code --- Models: --- class Category(models.Model): category = models.CharField(max_length=200) def __unicode__(self): return self.category class Game(models.Model): name = models.CharField(max_length=200) url = models.CharField(max_length=200) poster = models.ForeignKey(User, related_name='game_poster_set') postdate = models.DateTimeField(default=datetime.now) cats = models.ManyToManyField(Category) hits = models.IntegerField(default=0) post = models.BooleanField(default=False) --- Views: --- def submit(request): form = GameForm(request.POST or None) if form.is_valid(): game = form.save(commit=False) game.poster = request.user game.save() next = reverse('gamesite.games.views.favorites') return HttpResponseRedirect(next) return render_to_response( 'games/submit.html', {'form': form}, context_instance = RequestContext(request),) --- Forms: --- class GameForm(forms.ModelForm): name = forms.CharField(max_length=15, label='Name') url = forms.URLField(label='URL', initial='http://') class Meta: model = Game fields = ('name','url') Thanks! -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: ManyToMany Question
On Jun 11, 1:06 pm, LeeRisq wrote: > Programming with Django is unfortunately my job on the side right now, > so limited time for coding and testing. I know I could answer this > question myself by tinkering around, but don't have time. > > When you have a generic ManyToMany field specified between two models, > will Django automatically create a "through" table if I don't > explicitly tell it to? Logic tells me that it does, but the > documentation suggests otherwise unless I am losing it, which has been > known to happen. Thanks everyone. It always creates an intermediate table in the database, as there's no other way of modelling a many-to-many relation. However, it won't expose this via the ORM unless you create the intermediate model and use 'through'. -- DR. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
ManyToMany Question
Programming with Django is unfortunately my job on the side right now, so limited time for coding and testing. I know I could answer this question myself by tinkering around, but don't have time. When you have a generic ManyToMany field specified between two models, will Django automatically create a "through" table if I don't explicitly tell it to? Logic tells me that it does, but the documentation suggests otherwise unless I am losing it, which has been known to happen. Thanks everyone. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Yet another ManyToMany question ...
I have a model: class Employee(models.Model): name = models.CharField(maxlength = 100) reportsTo = models.ForeignKey('self', null=True, related_name='dependent_element') subordinates = models.ManyToManyField('self') I want to add the employee as a subordinate when the manager is defined, so long as they don't already exist: if manager.subordinates.filter(id = employee.id).count() == 0: manager.subordinates.add(employee) But this doesn't work ... it always returns count = 0. I think the problem is I'm attempting to ask "Give me the count of all of the subordinates of where subordinate ID = employee ID" but what I'm actually asking is: "Give me the count of all of the subordinates of where subordinate join table ID = employee ID" How should I phrase this? I couldn't see the answer in the archives (closest was http://groups.google.ca/group/django-users/browse_thread/thread/af75f41acda01ae5/5ac9f540627d4573?lnk=gst&q=many+to+many+self&rnum=1#5ac9f540627d4573) Thx Sandy --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: ManyToMany Question
{{ post.assoc_cats.all|join:", " }} does the right thing. Thank you. BTW: The RemovingTheMagic Document says change __repr__ to __str__ so i've done it. IMO it's a bit confusing. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: ManyToMany Question
chrisk wrote: >class Post(models.Model): >... > assoc_cats = models.ManyToManyField(Category) >... > >In my template {% for post in latest %}... works fine, but if i try to >use the Many-to-many related lookup {{ post.assoc_cats.all }} i get an >empty list. > > I wast about to answer something similar to Jay Parlar bu took a minute to check it.. And to my own wonder such construction really tries to output a list while I thought it should output something like that is swallowed by HTML as an unknown tag. However this list cosists of repr() of its values. So if you have a list with only one value and your assoc_cats don't have __repr__ you will get this: "[ ]" So to have a real list of strings you should either cycle through the queryset or just join it: {{ post.assoc_cats.all|join:", " }} It woul work if your assoc_cats do have __str__ method. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: ManyToMany Question
On 5/27/06, chrisk <[EMAIL PROTECTED]> wrote: > > Hi * > > I'm on trunk and using 'CookBookCategoryDataModelPostMagic'. > > In my models file i have this: > > class Post(models.Model): > ... >assoc_cats = models.ManyToManyField(Category) > ... > > In my template {% for post in latest %}... works fine, but if i try to > use the Many-to-many related lookup {{ post.assoc_cats.all }} i get an > empty list. > > If i import my models in the shell post.assoc_cats.all() shows the > right things. > > I have no idea what i can do now. I would be grateful for each > assistance. > > I don't think that {{post.assoc_cats.all}} is what you *really* want to do. More likely, you'd want something like: {% for post in latest %} {% for cat in post.assoc_cats.all %} {{ cat.name }} {% endfor %} {% endfor %} I don't think that QuerySets (which are what are returned by post.assoc_cats.all) can be represented directly in a template. Jay P. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
ManyToMany Question
Hi * I'm on trunk and using 'CookBookCategoryDataModelPostMagic'. In my models file i have this: class Post(models.Model): ... assoc_cats = models.ManyToManyField(Category) ... In my template {% for post in latest %}... works fine, but if i try to use the Many-to-many related lookup {{ post.assoc_cats.all }} i get an empty list. If i import my models in the shell post.assoc_cats.all() shows the right things. I have no idea what i can do now. I would be grateful for each assistance. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---