django-views

ExtractYear and ExtractMonth returning None in Django

删除回忆录丶 提交于 2019-12-23 03:17:27
问题 I am trying to do group my data on the basis of Year, Month and a Column Value. The Query is FeedbackData.objects.annotate(year=ExtractYear('created'), month=ExtractMonth('created')).values('year','month','start_operator_alias').annotate(dcount=Count('*')).values('year', 'month','start_operator_alias','dcount') The result that I am getting is: <QuerySet [{'year': None, 'start_operator_alias': 7, 'dcount': 12858, 'month': None}, {'year': None, 'start_operator_alias': 2, 'dcount': 185042,

How to perform pagination for context object in django?

爱⌒轻易说出口 提交于 2019-12-23 02:28:36
问题 I have tried something like this in views.py: class HomePage(TemplateView): template_name = "clouderp/index.html" def get_context_data(self, **kwargs): context = super(HomePage, self).get_context_data(**kwargs) qs = Blog.objects.all() context['blog_list'] = qs page = self.request.GET.get('page') paginator = Paginator(qs, 4) try: users = paginator.page(page) except PageNotAnInteger: users = paginator.page(1) except EmptyPage: users = paginator.page(paginator.num_pages) context['users'] = users

Expiring a view-cache in Django 1.3.1

独自空忆成欢 提交于 2019-12-23 01:44:16
问题 I'm trying to expire a view-level cache on a model's post_save (that was set via https://docs.djangoproject.com/en/1.3/topics/cache/?from=olddocs#the-per-view-cache). I did some googling and found this answer here on SO: Expire a view-cache in Django? but it's not working for me. I asked around in the #django room on freenode and the consensus was that this was probably due to the recent caching changes made in 1.3 Does anyone have any idea on how I can wipe out the cache entry for a model

Django User foreign key in View vs in model.save() method

不问归期 提交于 2019-12-23 01:41:31
问题 I have the following model (simplified): class Candidate(models.Model): """ Model for candidate clients """ # fields general_category = models.ForeignKey('GeneralCategory', related_name='candidate', null=True, blank=True, # default=1, verbose_name='Γενική Κατηγορία',) brand_name = models.CharField(max_length=160, blank=True, verbose_name='Επωνυμία') creation_date = models.DateTimeField(null=True, blank=True, verbose_name='Πρώτη καταχώρηση') last_edited = models.DateTimeField(null=True, blank

Python - Update a user in Django 2.2

烈酒焚心 提交于 2019-12-23 00:26:13
问题 I'm working on a project using Python(3.7) and Django(2.2) in which I have implemented models for multiple types of users by extending the base User model. Now I need to implement a way to allow the admin user to edit a user ( can't use the default Django admin). So, I have utilized the MultiModelForm to combine multiple forms on a single template, on the get request the form is loading properly with data populated. Here's what I have done so far: From models.py : class User(AbstractBaseUser,

django - get list of objects by filtering a list of objects

落爺英雄遲暮 提交于 2019-12-22 14:53:25
问题 I am creating a user activity streams. models for activity: class Activity(models.Model): actor = models.ForeignKey(User) action = models.CharField(max_length=100) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') pub_date = models.DateTimeField(auto_now_add=True, auto_now=False) model for Relationship: class Person(models.Model): user = models.OneToOneField(User) relationships =

Use serializer of model having foreign key to do CRUD on parent table in Django Rest Framework

走远了吗. 提交于 2019-12-22 13:02:52
问题 In my API, I have two models Question and Option as shown below class Question(models.Model): body = models.TextField() class Options(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) option = models.CharField(max_length=100) is_correct = models.SmallIntegerField() While creating a question it would be nicer the options can be created at the same time. And already existed question should not be created but the options can be changed if the options are different

Django Rest Framework: How to pass data to a nested Serializer and create an object only after custom validation

南楼画角 提交于 2019-12-22 12:23:33
问题 I have two models as: class Book(AppModel): title = models.CharField(max_length=255) class Link(AppModel): link = models.CharField(max_length=255) class Page(AppModel): book= models.ForeignKey("Book",related_name="pages",on_delete=models.CASCADE) link = models.ForeignKey("Link", related_name="pages", on_delete=models.CASCADE) page_no = models.IntegerField() text = models.TextField() and serializers class LinkSerializer(serializers.ModelSerializer): class Meta: model = Link fields = ['link']

Django two views on a webpage

99封情书 提交于 2019-12-22 11:28:11
问题 Hello and thank you in advance, I am very much a Django/Python noobie. I just need guidance, not necessarily answers. I have read all the pertinent documentation and I can't seem to find a concise example of what I am trying to do. I have two views, one is a form and the other is a view that lists data contained in a database table. I am trying to display both these views on the same webpage page that is called by the user going to one URL listed in the URLS.py file. I am sure this is

Optimize code in Django - view ManyToMany as matrix

邮差的信 提交于 2019-12-22 10:47:13
问题 I'm trying to show user group permissions in Django and show them in a "Drupal" style like a matrix. It works, but it takes too long to make the query and paint it in the template. Is there some way to improve my code? view img up(accomplished),down(views and template.html) views : def GroupPermissionsView(request): title = "Groups Permissions" groups = Group.objects.all() permissions = Permission.objects.all() context = Context({ 'title': title, 'groups': groups, 'permissions': permissions,