django-views

Retrieve the same values ​whose data is there or exists and not the rest.In django

人走茶凉 提交于 2021-01-07 02:56:27
问题 I want to have a data that does not have any empty value in database or in row. I wrote like this in my code. faq = FAQ.objects.values('question','answer','field_id') this the output in my terminal {'question': None, 'answer': None, 'field_id': None} {'question': 'Test question', 'answer': '<p>Testsaddsf description</p>\r\n', 'field_id': 'TestTest'} i don't want None value data. 回答1: You can filter with the __isnull lookup [Django-doc]: faq = FAQ.objects.filter( question __isnull=False ,

How best to capture variables from within a for-loop in Django template

江枫思渺然 提交于 2021-01-07 02:48:49
问题 I have two querysets: type and age_group . type queryset: <QuerySet [<Type: Cat>, <Type: Dog>, <Type: Other>]> age_group queryset: <QuerySet [<AgeGroup: Young>, <AgeGroup: Baby>, <AgeGroup: Adult>, <AgeGroup: Senior>]> I loop through these from within my template form so that I can grab the pk when one has been selected, but I cannot capture the variable from within the for loop. How do I capture a variable from within a for loop when using Django? I want to capture pk for type and pk for age

Django forms - create a form for each object in model, and then save to the corresponding PK

回眸只為那壹抹淺笑 提交于 2021-01-07 02:41:46
问题 I'm new to django (and programming in general). Sorry if I used the wrong vocabulary (and please correct me). This is what i'm trying to do: I have this model: Class A(models.Model): name = models.CharField(max_length=100, null=True, default='John') first_number = models.FloatField(blank=True, default=1) second_number = models.FloatField(blank=True, default=2) third_number = models.FloatField(blank=True, default=3) And I have this form: class Numbers(ModelForm): class Meta: model = A fields =

Not able to update an item in Django

大兔子大兔子 提交于 2021-01-07 01:46:13
问题 I am trying to update the Bar Information for users by getting their details and setting the Bar instance. But every time I click on the link on my dashboard it redirects me to the dashboard which is what I used for the else statement if it is not a post method. view.py def UpdateUserBar(request): user = request.user.id bar = Bar.objects.get(user_id=user) form = UpdateBar(instance=bar) if request.method == 'POST': form = UpdateBar(request.POST,request.FILES, instance=bar) if form.is_valid():

Not able to update an item in Django

孤街浪徒 提交于 2021-01-07 01:43:48
问题 I am trying to update the Bar Information for users by getting their details and setting the Bar instance. But every time I click on the link on my dashboard it redirects me to the dashboard which is what I used for the else statement if it is not a post method. view.py def UpdateUserBar(request): user = request.user.id bar = Bar.objects.get(user_id=user) form = UpdateBar(instance=bar) if request.method == 'POST': form = UpdateBar(request.POST,request.FILES, instance=bar) if form.is_valid():

How to post OneToOne field in django rest-framework using overwrite create method

落爺英雄遲暮 提交于 2021-01-05 07:31:15
问题 I am trying to override create method to make a post request but i am stuck, here is my code class Order(models.Model): street_number = models.PositiveIntegerField(blank=True, null=True) street_name = models.CharField(max_length=250, null=True, blank=True) class Seller(models.Model): order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='seller_order',blank=True, null=True) first_name = models.CharField(max_length=250, null=True, blank=True) last_name = models.CharField

How to post OneToOne field in django rest-framework using overwrite create method

允我心安 提交于 2021-01-05 07:24:24
问题 I am trying to override create method to make a post request but i am stuck, here is my code class Order(models.Model): street_number = models.PositiveIntegerField(blank=True, null=True) street_name = models.CharField(max_length=250, null=True, blank=True) class Seller(models.Model): order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='seller_order',blank=True, null=True) first_name = models.CharField(max_length=250, null=True, blank=True) last_name = models.CharField

'module' object is not iterable when running django website to the server

不想你离开。 提交于 2021-01-04 12:31:45
问题 i wanted to run my django website to their server so i open cmd and go to manage.py directory : C:\Users\computer house>cd desktop/newproject then i type this code : python manage.py runserver but i got this error : Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03BE5A08> Traceback (most recent call last): File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py", line 542, in url_patterns iter

'module' object is not iterable when running django website to the server

℡╲_俬逩灬. 提交于 2021-01-04 12:31:32
问题 i wanted to run my django website to their server so i open cmd and go to manage.py directory : C:\Users\computer house>cd desktop/newproject then i type this code : python manage.py runserver but i got this error : Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x03BE5A08> Traceback (most recent call last): File "C:\Users\computer house\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\resolvers.py", line 542, in url_patterns iter

Postgre SQL ignore the filtering condition if the value is null

青春壹個敷衍的年華 提交于 2021-01-04 08:05:42
问题 I have the following three variables passed to the query A,B and C . A, B and C can take any values including null. When I run the below queryset, it should ignore the condition if the value in A,B or C is null queryset = User.objects.values().filter(A_name=A, B_name=B, C_name =C) For example, if C value passed in null then the query should behave like queryset = User.objects.values().filter(A_name=A, B_name=B) And if C and A value passed in null then the query should behave like queryset =