-1

i need some help. I have a model "Event" with manytomanyfield "users":

class Event(models.Model):
    name = models.CharField(max_length=26)
    description = models.CharField(max_length=200)
    date = models.DateField()
    user = models.ForeignKey(User)
    users = models.ManyToManyField(User, related_name="event", blank=True)
    image = models.ImageField(
        upload_to='images/',
        default='images/default.png'
    )

So, manytomany intermediary table has two fields: user_id and event_id. How to count how many event_id = 1 or event_id = 2... are in this table? Thanks

worm2d
  • 159
  • 1
  • 2
  • 14
  • I don't really understand this model. Why do you have `user`, and `users` – zEro Mar 21 '16 at 08:11
  • @zEro `user` table is for owner id of this event, `users` is for "follow" just like in this question - http://stackoverflow.com/questions/36063984/django-how-to-follow-some-object-not-user/36064530#36064530 – worm2d Mar 21 '16 at 08:17
  • not relevant to this question, but it is really helpful to have meaningful names for your model fields. It would be far clearer if your fields were named `owner` and `followers`. And you should also have a related name for the foreign key, say `related_name="owned_events"` – zEro Mar 21 '16 at 08:21
  • If you copy your question title into google you get 120,000 results, did none of them help? – Sayse Mar 21 '16 at 08:25

1 Answers1

1

All you'd need is the below model for Event:

class Event(models.Model):
        name = models.CharField(max_length=26)
        description = models.CharField(max_length=200)
        date = models.DateField()
        user = models.ManyToMany(User, related_name="events")
        image = models.ImageField(
            upload_to='images/',
            default='images/default.png'
        )

And then from a object of User (say logged_in_user) you can make calls such as logged_in_user.events.all() to get all the events. Or if you just need event id's then logged_in_user.events.values_list('id', flat=True)

If you just want a count, then it should be logged_in_user.events.count(). As you can see, you can treat events the same as any other manager (like objects on your user model).

If you need the count of users participating in a single event with event_id. User this: Event.objects.get(id=event_id).users.count()

zEro
  • 1,255
  • 14
  • 24
  • `logged_in_user.events.count()` returns a number of `events` where `user = logged_in_user` I dont need this. I need a number of `users` in manytomany intermediary table where `event_id` is equal to some value. Sorry if I put it inaccurately – worm2d Mar 21 '16 at 08:22
  • That wasn't very clear from your question. And anyway, in that case `Event.objects.get(id=event_id).users.count()` would give you just that – zEro Mar 21 '16 at 08:25
  • great thanks! Now i see, it works. Sorry, I have a little problem with understanding requests to ManyToMantField. – worm2d Mar 21 '16 at 08:28
  • everybody had them! :) do you mind +1 the answer if it worked for you? – zEro Mar 21 '16 at 08:28
  • @zEro - 1. You need [15 rep](http://stackoverflow.com/help/privileges) to upvote. and 2. Its generally discouraged to beg for upvotes, posts should be voted according to their perceived quality – Sayse Mar 21 '16 at 08:31
  • @zEro Sorry i registered here for a couple of days ago and dont have enough reputation for +1, but i'll do it later when my rep=15 :) – worm2d Mar 21 '16 at 08:33