django-orm

how to get group name using User in django

随声附和 提交于 2021-02-20 04:47:28
问题 I am stuck in django's group module can any one tell me how to solve it. my problem is now i'm dispalying the username of the logged-in user, i want to display group-name and the profiles present in that group along with the username.and my views are like this, def user(request): context = {'user': request.user} return render_to_response('username.html', context, context_instance=RequestContext(request)) now what do I need to add in order to achieve my requirement forms.py class LoginForm

Django ManyToMany field through with extra fields does not show on both relations

南笙酒味 提交于 2021-02-09 17:59:47
问题 I have a class Assembly class Assembly(models.Model): room = models.ForeignKey("Room", related_name="assemblies") name = models.CharField(max_length=200) number = models.IntegerField() position = models.CharField(max_length=200, blank=True) components = models.ManyToManyField("material.Component", through="m2m_Assembly_Components") connections = models.ManyToManyField("Assembly", through="Connection") category = models.ForeignKey("Category", default=0) notes = models.TextField(blank=True)

Aggregate hourly count based on “HOUR” in datetime field in Django

妖精的绣舞 提交于 2021-02-08 10:20:30
问题 I am using Django 2.2 along with MySQL. I have the following query: Here's the model for the reference : class TrackCount(models.Model): people_count_obj_id = models.CharField(primary_key=True, default=uuid.uuid4, editable=False, max_length=255) recorded_at = models.DateTimeField() camera = models.ForeignKey(Camera, on_delete=models.PROTECT) store = models.ForeignKey(Store, on_delete=models.PROTECT) people_count_in = models.IntegerField() people_count_out = models.IntegerField() created_at =

Aggregate hourly count based on “HOUR” in datetime field in Django

天大地大妈咪最大 提交于 2021-02-08 10:19:53
问题 I am using Django 2.2 along with MySQL. I have the following query: Here's the model for the reference : class TrackCount(models.Model): people_count_obj_id = models.CharField(primary_key=True, default=uuid.uuid4, editable=False, max_length=255) recorded_at = models.DateTimeField() camera = models.ForeignKey(Camera, on_delete=models.PROTECT) store = models.ForeignKey(Store, on_delete=models.PROTECT) people_count_in = models.IntegerField() people_count_out = models.IntegerField() created_at =

Django: Most efficient way to create a nested dictionary from querying related models?

和自甴很熟 提交于 2021-02-08 04:59:37
问题 In Django, what is the most efficient way to create a nested dictionary of data from querying related and child models? For example, if I have the following models: Parent Children Pets I've seen django's model_to_dict method, and that's pretty cool, so I imagine I could loop through each level's queryset and create a bunch of DB calls on each level, for each instance, but is there a better way? For example, could "prefetch_related" be used to get all three tiers as it is used to get two

Django IntegerField returning string(!) - how to coerce to int?

≯℡__Kan透↙ 提交于 2021-02-07 14:41:32
问题 I have an IntegerField declared as below on a model: amount = models.IntegerField() When accessing it, it sometimes returns a string. The proximate cause for this is that it has had a string assigned to it. So far, so unmysterious. It also returns a string even after it has been saved. This strikes me as a little surprising: it would be nice if IntegerField coerced its value to an integer on assignment (or, at the latest, on save), so the user could rely on it being an integer. (My

Django IntegerField returning string(!) - how to coerce to int?

我怕爱的太早我们不能终老 提交于 2021-02-07 14:39:29
问题 I have an IntegerField declared as below on a model: amount = models.IntegerField() When accessing it, it sometimes returns a string. The proximate cause for this is that it has had a string assigned to it. So far, so unmysterious. It also returns a string even after it has been saved. This strikes me as a little surprising: it would be nice if IntegerField coerced its value to an integer on assignment (or, at the latest, on save), so the user could rely on it being an integer. (My

order_by on Many-to-Many field results in duplicate entries in queryset

帅比萌擦擦* 提交于 2021-02-07 07:21:59
问题 I am attempting to perform an order_by based a m2m field, but it ends up creating duplicate entries in my queryset. I have been searching through the django documentation and related questions on stack exchange, but I haven't been able to come up with any solutions. Models: class WorkOrder(models.Model): ... appointment = models.ManyToManyField(Appointment, null=True, blank=True, related_name = 'appointment_from_schedule') ... class Appointment(models.Model): title = models.CharField(max

Django filter objects with at least one many-to-many having attribute of value

一曲冷凌霜 提交于 2021-02-07 02:56:54
问题 I'm looking to do a complex filter using Django's ORM. Models: class Book(models.Model): title = models.TextField() bestseller = models.BooleanField(default=False) class Author(models.Model): name = models.TextField() books = models.ManytoManyField(Book) How would I query for all authors who have at least one best-selling book? Query: best_authors = Author.objects.filter(<relevant filter>) Edit: According to the documentation, the following should work: best_authors = Author.objects.filter

Django select_related in reverse

瘦欲@ 提交于 2021-02-06 10:49:21
问题 I have the following model: class Campaign(models.Model): some_campaign_field = models.CharField() class Position(models.Model): campaign = models.ForeignKey(Campaign) some_position_field = models.CharField() class Trade(models.Model): position = models.ForeignKey(Position) some_trade_field = models.CharField() In other words, I have Campaigns which can have multiple Positions. In turn each position within the campaign can have multiple Trades. Is there an efficient way (ie: minimal database