Re: Views & Template issue

2018-09-12 Thread Andréas Kühne
Hi Mikko,

It works correctly the way you have written it. Look at the code you have
written:
t = time.time()

def index(request):
newstest = NewsData.objects.filter(created__gt= t -
300).order_by('-created')
newstest1 = NewsData.objects.filter(created__lt= t -
300).order_by('-created')
args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
than 5 min']*}


You get the time when the server is started (the time.time() function is
only run once) and then you never call that function again.
If you want to check the time every time you load the page you should do it
like this:

def index(request):
t = time.time()
newstest = NewsData.objects.filter(created__gt= t -
300).order_by('-created')
newstest1 = NewsData.objects.filter(created__lt= t -
300).order_by('-created')
args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
than 5 min']*}

Also you should not use the basic time fileds in python when querying the
database, but use fields that include the current timezone. In that case
you should add this instead:

from django.utils.timezone import now

def index(request):
t = now()
newstest = NewsData.objects.filter(created__gt= t -
300).order_by('-created')
newstest1 = NewsData.objects.filter(created__lt= t -
300).order_by('-created')
args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
than 5 min']*}

Regards,

Andréas


Den ons 12 sep. 2018 kl 01:46 skrev Mikko Meronen <
mikkovillemero...@gmail.com>:

> Hi, I still have a small issue here and couldn't figure it out.
>
> I want to divide my items based on the time created. The division point is
> 5 minutes and  I want that point to be seen in my webpage. However when I
> made the division point, Django doesn't update it (the items that become
> older than 5 mins do not move to the older group) until I restart the
> server. Otherwise the new items will aggregate to the webpage as supposed.
> Is there anyway in Django to make this work in views.py and templates?
>
> Otherwise I guess I should try to figure out if it is possible in SQL side
> by creating more tables based on the creation time and updating the tables
> there. Now all my items are in a single table.
>
> - Mikko
>
> t = time.time()
>
> def index(request):
> newstest = NewsData.objects.filter(created__gt= t -
> 300).order_by('-created')
> newstest1 = NewsData.objects.filter(created__lt= t -
> 300).order_by('-created')
> args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
> than 5 min']*}
> return render(request, "news/index.html", args)
>
>
> 
> 
> 
> 
> {% for news in newstest %}
>  {{ news.title }} 
> {{ news.published }} {{ news.publisher }} {{ news.section }} {{
> news.country }}
> 
> {% endfor %}
> 
> 
> 
> 
> **
> * *
> * *
> * *
> * {% for c in newstest2 %}*
> *  {{c}} *
> * *
> * {% endfor %}*
> * *
> * *
> * *
> * *
>
> 
> 
> 
> 
> {% for news1 in newstest1 %}
>  {{ news1.title }} 
> {{ news1.published }} {{ news1.publisher }} {{ news1.section }} {{
> news1.country }}
> 
> {% endfor %}
> 
> 
> 
> 
>
> su 9. syysk. 2018 klo 17.21 Mikko Meronen (mikkovillemero...@gmail.com)
> kirjoitti:
>
>> Thank you.
>>
>> -Mikko
>>
>> su 9. syysk. 2018 klo 16.49 Jason (jjohns98...@gmail.com) kirjoitti:
>>
>>> You've defined two views which pass in different querysets to be
>>> rendered.
>>>
>>> so if you hit index, you get the first queryset rendered but not the
>>> second because newstest1 is not rendered at all.  and vice versa.
>>>
>>> solution is to add newstest1 to index and pass both querysets as context
>>> to the render.  eg
>>>
>>> return render(request, 'news/index.html', {'newstest': newstest,
>>> 'newstest1': newstest1})
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To post to this group, send email to django-users@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/6ebc2322-5bb5-4809-a6f3-1adf94e92168%40googlegroups.com
>>> 
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAGD0jjLOtkTd2PRpM38Z_ypUDJ%2BeEmXc

Django restapi get post

2018-09-12 Thread TimT Vogt
Hi Group 
I am making an Django backend to out data with a rest API to angular frontend.

The rest-API/ GET works😀.
The POST: do I ajust the current serializer?
Or do I need to create another serializer in ABN New app with a recieving model?

Verstuurd vanaf mijn iPhone

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/96859EB9-17EF-49C4-ADD6-3D912749FF5B%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Possible bug in makemigrations

2018-09-12 Thread Michel van Leeuwen
Hi all,

I found out a possible bug in the django makemigrations (i am not sure
though) in django 1.11.15 (no time to test this will happen in django
2.x as well).

When deleting a field that's also in the model meta index_together the
migration created by makemigrations gave an error.


In the migrations created by makemigrations

the order of migration is first to delete the field

  migrations.RemoveField(...)

and than

  migrations.AlterIndexTogether(...)


The migration did not run on my dev environment (sqllite), but after
changing this order:

first alter index together and than remove filed

It worked.

so the order in makemigrations should be reversed i guess

Did not test this on the development server, which is mysql (i used the
manually changed migration on mysql right away).

but if so, i suppose this order should be changed.

My problem is solved, but might save some puzzling time in the future
for others?

But not sure this si a bug, and if i should report it.


no time to reproduce right now, but should be easy to reproduce, just
make an index_together, remove one of the fields in the index together,
and makemigrations

By the way: using django 1.11.15 in this project. Not sure how this
reacts in django 2.x


-- 
Met vriendelijke groet / With kind regards /
Med venlig Hilsen / Mit freundlichen Grüssen

Michèl van Leeuwen
Digital problem solver
mic...@iamit.nl
+31 (0)6 387 58 163
www.iamit.nlPostbus 3363 2601
DJ Delft
+31 (0)15 20 230 20


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/063feb2f-479a-bd88-03d2-01153ea17fbf%40iamit.nl.
For more options, visit https://groups.google.com/d/optout.


Re: Views & Template issue

2018-09-12 Thread Mikko Meronen
Thank you, works well now :)

ke 12. syysk. 2018 klo 11.37 Andréas Kühne (andreas.ku...@hypercode.se)
kirjoitti:

> Hi Mikko,
>
> It works correctly the way you have written it. Look at the code you have
> written:
> t = time.time()
>
> def index(request):
> newstest = NewsData.objects.filter(created__gt= t -
> 300).order_by('-created')
> newstest1 = NewsData.objects.filter(created__lt= t -
> 300).order_by('-created')
> args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
> than 5 min']*}
>
>
> You get the time when the server is started (the time.time() function is
> only run once) and then you never call that function again.
> If you want to check the time every time you load the page you should do
> it like this:
>
> def index(request):
> t = time.time()
> newstest = NewsData.objects.filter(created__gt= t -
> 300).order_by('-created')
> newstest1 = NewsData.objects.filter(created__lt= t -
> 300).order_by('-created')
> args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
> than 5 min']*}
>
> Also you should not use the basic time fileds in python when querying the
> database, but use fields that include the current timezone. In that case
> you should add this instead:
>
> from django.utils.timezone import now
>
> def index(request):
> t = now()
> newstest = NewsData.objects.filter(created__gt= t -
> 300).order_by('-created')
> newstest1 = NewsData.objects.filter(created__lt= t -
> 300).order_by('-created')
> args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
> than 5 min']*}
>
> Regards,
>
> Andréas
>
>
> Den ons 12 sep. 2018 kl 01:46 skrev Mikko Meronen <
> mikkovillemero...@gmail.com>:
>
>> Hi, I still have a small issue here and couldn't figure it out.
>>
>> I want to divide my items based on the time created. The division point
>> is 5 minutes and  I want that point to be seen in my webpage. However when
>> I made the division point, Django doesn't update it (the items that become
>> older than 5 mins do not move to the older group) until I restart the
>> server. Otherwise the new items will aggregate to the webpage as supposed.
>> Is there anyway in Django to make this work in views.py and templates?
>>
>> Otherwise I guess I should try to figure out if it is possible in SQL
>> side by creating more tables based on the creation time and updating the
>> tables there. Now all my items are in a single table.
>>
>> - Mikko
>>
>> t = time.time()
>>
>> def index(request):
>> newstest = NewsData.objects.filter(created__gt= t -
>> 300).order_by('-created')
>> newstest1 = NewsData.objects.filter(created__lt= t -
>> 300).order_by('-created')
>> args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
>> than 5 min']*}
>> return render(request, "news/index.html", args)
>>
>>
>> 
>> 
>> 
>> 
>> {% for news in newstest %}
>>  {{ news.title }} 
>> {{ news.published }} {{ news.publisher }} {{ news.section }} {{
>> news.country }}
>> 
>> {% endfor %}
>> 
>> 
>> 
>> 
>> **
>> * *
>> * *
>> * *
>> * {% for c in newstest2 %}*
>> *  {{c}} *
>> * *
>> * {% endfor %}*
>> * *
>> * *
>> * *
>> * *
>>
>> 
>> 
>> 
>> 
>> {% for news1 in newstest1 %}
>>  {{ news1.title }} 
>> {{ news1.published }} {{ news1.publisher }} {{ news1.section }} {{
>> news1.country }}
>> 
>> {% endfor %}
>> 
>> 
>> 
>> 
>>
>> su 9. syysk. 2018 klo 17.21 Mikko Meronen (mikkovillemero...@gmail.com)
>> kirjoitti:
>>
>>> Thank you.
>>>
>>> -Mikko
>>>
>>> su 9. syysk. 2018 klo 16.49 Jason (jjohns98...@gmail.com) kirjoitti:
>>>
 You've defined two views which pass in different querysets to be
 rendered.

 so if you hit index, you get the first queryset rendered but not the
 second because newstest1 is not rendered at all.  and vice versa.

 solution is to add newstest1 to index and pass both querysets as
 context to the render.  eg

 return render(request, 'news/index.html', {'newstest': newstest,
 'newstest1': newstest1})

 --
 You received this message because you are subscribed to the Google
 Groups "Django users" group.
 To unsubscribe from this group and stop receiving emails from it, send
 an email to django-users+unsubscr...@googlegroups.com.
 To post to this group, send email to django-users@googlegroups.com.
 Visit this group at https://groups.google.com/group/django-users.
 To view this discussion on the web visit
 https://groups.google.com/d/msgid/django-users/6ebc2322-5bb5-4809-a6f3-1adf94e92168%40googlegroups.com
 
 .
 For more options, visit https://groups.google.com/d/optout.

>>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> emai

y its not working

2018-09-12 Thread Mohamad Rumaiz
TemplateDoesNotExist at /

personal/home.html

Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 2.0.5
Exception Type: TemplateDoesNotExist
Exception Value: 

personal/home.html

Exception Location: C:\Python36\lib\site-packages\django\template\loader.py 
in get_template, line 19
Python Executable: C:\Python36\python.exe
Python Version: 3.6.0
Python Path: 

['C:\\Users\\RUMAIZ\\Desktop\\New folder (2)\\mysite',
 'C:\\Python36\\python36.zip',
 'C:\\Python36\\DLLs',
 'C:\\Python36\\lib',
 'C:\\Python36',
 'C:\\Users\\RUMAIZ\\AppData\\Roaming\\Python\\Python36\\site-packages',
 'C:\\Python36\\lib\\site-packages',
 'C:\\Python36\\lib\\site-packages\\win32',
 'C:\\Python36\\lib\\site-packages\\win32\\lib',
 'C:\\Python36\\lib\\site-packages\\Pythonwin']

Server time: Wed, 12 Sep 2018 04:40:16 +

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2307b62f-3e5d-4186-a8f6-da5f90dd280b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Different results of running pure SQL query from Django and from PGAdmin

2018-09-12 Thread Pierre-Louis K.
Hello Bill,

I managed to solve this in the end, the issue was that a cursor created in 
Django would enforce TIME_ZONE settings in the PostgreSQL session, where by 
default it is set to 'UTC' in my case. This means that all date 
manipulation was impacted, hence the unexpected results.
The work around is to set the SQL session settings just before running the 
query with something like:
cursor.execute("SET TIMEZONE to UTC")

There is a nice explanation about this here: 
http://blog.untrod.com/2016/08/actually-understanding-timezones-in-postgresql.html
Quoting : 
"For instance, when Django connects to Postgres, it 'ensures timezone 
'
 
and slaps the application time zone onto the psycopg2 connection so you get 
back timestamps in the same timezone you put them in (assuming you are 
inserting local times). That's clever, but can lead to incredibly confusion 
if you are trying to compare results from the same database, but via 2 
different connections, which are set to two different time zones."

And a similar bug ticket : https://code.djangoproject.com/ticket/23524

Hope this will help someone else,

Le mardi 11 septembre 2018 17:19:38 UTC+2, Bill-Torcaso-Oxfam a écrit :
>
>
> I'd be interested to see a printout of 'columns' and 'cursor.description'.
>
> One explanation would be that your for-loop is not actually accessing the 
> data that you think it is, or that your query is not actually fetching the 
> data that you think it is.  
>
> No criticism of your query implied - just trying to get more visibility 
> into the actual results.  
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ac2e7a1c-a1e6-411c-a3ca-a1746e531e0c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


aldryn events paginate

2018-09-12 Thread Денис
i am using aldryn event in django-cms project.
how to configure pagination in the plugin "list"?
tenx

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/639ed0a8-5c59-4323-8feb-fd1e12a3f79f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Possible bug in makemigrations

2018-09-12 Thread Simon Charette
Hello Michèl,

I'm pretty sure you hit #28862[0] which should be fixed in Django 2.2.

Here's the PR resolving it if you're curious about it[1].

Cheers,

[0] https://code.djangoproject.com/ticket/28862
[1] https://github.com/django/django/pull/10178

Le mercredi 12 septembre 2018 06:29:30 UTC-4, Michel van Leeuwen a écrit :
>
> Hi all,
>
> I found out a possible bug in the django makemigrations (i am not sure 
> though) in django 1.11.15 (no time to test this will happen in django 2.x 
> as well).
>
> When deleting a field that's also in the model meta index_together the 
> migration created by makemigrations gave an error.
>
>
> In the migrations created by makemigrations
>
> the order of migration is first to delete the field
>
>   migrations.RemoveField(...)
>
> and than
>
>   migrations.AlterIndexTogether(...)
>
>
> The migration did not run on my dev environment (sqllite), but after 
> changing this order:
>
> first alter index together and than remove filed
>
> It worked.
>
> so the order in makemigrations should be reversed i guess
>
> Did not test this on the development server, which is mysql (i used the 
> manually changed migration on mysql right away).
>
> but if so, i suppose this order should be changed.
>
> My problem is solved, but might save some puzzling time in the future for 
> others?
>
> But not sure this si a bug, and if i should report it.
>
>
> no time to reproduce right now, but should be easy to reproduce, just make 
> an index_together, remove one of the fields in the index together, and 
> makemigrations
>
> By the way: using django 1.11.15 in this project. Not sure how this reacts 
> in django 2.x
>
>
> -- 
> Met vriendelijke groet / With kind regards /
> Med venlig Hilsen / Mit freundlichen Grüssen
>
> Michèl van Leeuwen
> Digital problem solver 
> mic...@iamit.nl 
> +31 (0)6 387 58 163
> www.iamit.nl Postbus 3363 2601
> DJ Delft
> +31 (0)15 20 230 20 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2cd33cc1-06d4-4e1a-8041-e7b8549d7331%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: y its not working

2018-09-12 Thread Mark Phillips
The Exception Type says the template does not exist. The Exception Value
says the missing template is personal/home.html.

The template personal/home.html does not exist or is not located where
django is looking for it.

Mark

On Tue, Sep 11, 2018 at 9:55 PM, Mohamad Rumaiz 
wrote:

> TemplateDoesNotExist at /
>
> personal/home.html
>
> Request Method: GET
> Request URL: http://127.0.0.1:8000/
> Django Version: 2.0.5
> Exception Type: TemplateDoesNotExist
> Exception Value:
>
> personal/home.html
>
> Exception Location: C:\Python36\lib\site-packages\django\template\loader.py
> in get_template, line 19
> Python Executable: C:\Python36\python.exe
> Python Version: 3.6.0
> Python Path:
>
> ['C:\\Users\\RUMAIZ\\Desktop\\New folder (2)\\mysite',
>  'C:\\Python36\\python36.zip',
>  'C:\\Python36\\DLLs',
>  'C:\\Python36\\lib',
>  'C:\\Python36',
>  'C:\\Users\\RUMAIZ\\AppData\\Roaming\\Python\\Python36\\site-packages',
>  'C:\\Python36\\lib\\site-packages',
>  'C:\\Python36\\lib\\site-packages\\win32',
>  'C:\\Python36\\lib\\site-packages\\win32\\lib',
>  'C:\\Python36\\lib\\site-packages\\Pythonwin']
>
> Server time: Wed, 12 Sep 2018 04:40:16 +
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/2307b62f-3e5d-4186-a8f6-da5f90dd280b%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEqej2ML-7SWybZ9grkuHmmcrxCipejrSAVnV3srYe8uAvB9DA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: username on URL with Class Based Views

2018-09-12 Thread Gear Crew
Do you know what's the specific problem in my code?

On Wed, 12 Sep 2018 at 22:05, Ricardo Cataldi 
wrote:

> nice to be here again :D
>
>
> This question is on stackoverflow, in here:
> https://stackoverflow.com/questions/52302052/django-url-custom-parameters-on-base-class-view.
> I have writen a class based views based on generic.base.view to get
> information about users profiles, that are comming from different models
> and db tables, that will be applied both in viewing some user information
> on a social network and by the own user on edditing his own profile. Here
> is the view:
>
>
> class ProfileView(PageTitleMixin, generic.View):template = 
> "common/profile.html"
> template_name = "Perfil"
> page_title = _('Profile')
> active_tab = 'profile'
> success_url = reverse_lazy('usrxtnsion:profile')
> def get_queryset(self):
> return User.objects.filter(username=self.kwargs['username'])
> def get_context_data(self, **kwargs):
> ctx = super().get_context_data(**kwargs)
> account_info = Profile.objects.get(pk=self.request.user.profile.pk)
> user_info = User.objects.get(pk=self.request.user.pk)
> ctx['user_fields'] = self.get_profile_fields(self.request.user)
> ctx['profile_fields'] = user_info.profile_set.all()
> ctx['address_fields'] = user_info.useraddress_set.all()
> ctx['account_fields'] = account_info.account_set.all()
> ctx['username'] = self.request.user.username
> return ctx
> def get_profile_fields(self, user):
> field_data = []
>
> # Check for custom user model
> for field_name in User._meta.additional_fields:
> field_data.append(
> self.get_model_field_data(user, field_name))
>
> # Check for profile class
> profile_class = get_profile_class()
> if profile_class:
> try:
> profile = profile_class.objects.get(user=user)
> except ObjectDoesNotExist:
> profile = profile_class(user=user)
>
> field_names = [f.name for f in profile._meta.local_fields]
> for field_name in field_names:
> if field_name in ('user', 'id'):
> continue
> field_data.append(
> self.get_model_field_data(profile, field_name))
>
> return field_data
> def get_model_field_data(self, model_class, field_name):
> """
> Extract the verbose name and value for a model's field value
> """
> field = model_class._meta.get_field(field_name)
> if field.choices:
> value = getattr(model_class, 'get_%s_display' % field_name)()
> else:
> value = getattr(model_class, field_name)
> return {
> 'name': getattr(field, 'verbose_name'),
> 'value': value,
> }
> def get(self, request, *args, **kwargs):
> user_form = SignupForm(instance=self
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALvPq4%2B4k4a30UjhPeUTLf5E-2TwkBQ%2ByVXzDiat3wPRggTAVQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django restapi get post

2018-09-12 Thread PASCUAL Eric
Hi,


It depends if the structure of the data you want to post is the same as the one 
your retrieve with a GET, and if it fits with the underlying model. In this 
case, the same serializer can be used for both operations. Pay attention 
however that things can be more complex if the involved model uses relations. 
In this case, using the same serializer is not always applicable.


If not possible, you'll need to define one for data retrieval (you can 
configure it so that all fields are read-only to be sure it will not be used 
incorrectly) and another one to model the data you'll send for a POST.


Eric


From: django-users@googlegroups.com  on behalf 
of TimT Vogt 
Sent: Wednesday, September 12, 2018 11:04:39 AM
To: django-users@googlegroups.com
Subject: Django restapi get post

Hi Group
I am making an Django backend to out data with a rest API to angular frontend.

The rest-API/ GET works😀.
The POST: do I ajust the current serializer?
Or do I need to create another serializer in ABN New app with a recieving model?

Verstuurd vanaf mijn iPhone

--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/96859EB9-17EF-49C4-ADD6-3D912749FF5B%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/DB7P193MB0426614C49AF4D4ADFB005B18C1B0%40DB7P193MB0426.EURP193.PROD.OUTLOOK.COM.
For more options, visit https://groups.google.com/d/optout.


How do I make a field optional to enter, in a django ModelForm?

2018-09-12 Thread Joel Mathew
Scenario:
I instantiate a ModelForm and pass it to a template which displays the
form. When POST is submitted, code tries to search the database by any
of the given inputs. I dont require all inputs to be entered as in the
Model. I just need one (or more, if user desires to do an AND search)
to be entered.

Question: How can I make any of the ModelForm fields optional, where
in the Model, the field isnt optional. The field isnt optional in the
Model because I have another ModelForm based on the same Model, where
user is required to enter all his details.

My model:

class customer(models.Model):
# Need autoincrement, unique and primary
cstid = models.AutoField(primary_key=True, unique=True)
name = models.CharField(max_length=35)
age=models.IntegerField()
gender_choices = (('male', 'Male'),
('female', 'Female'))
gender = models.CharField(
choices=gender_choices, max_length=10, default='male')
maritalstatus_choices = ( ('married', 'Married'),
('unmarried', 'Unmarried'))
maritalstatus = models.CharField(
choices=maritalstatus_choices, max_length=10, default='Unmarried')
mobile = models.CharField(max_length=15, default='')
alternate = models.CharField(max_length=15, default='')
email = models.CharField(max_length=50, default='', blank=True)
address = models.CharField(max_length=80, default='', blank=True)
city = models.CharField(max_length=25, default='', blank=True)
occupation = models.CharField(max_length=25, default='', blank=True)
bloodgroup_choices = (('apos', 'A+'),
('aneg', 'A-'),
('bpos', 'B+'),
('bneg', 'B-'),
('opos', 'O+'),
('oneg', 'O-'),
('abpos', 'AB+'),
('abneg', 'AB-'),
('unspecified', '-')
)
bloodgroup = models.CharField(choices=bloodgroup_choices,
max_length=3, default='-', blank=True)
class Meta:
unique_together = ["name", "mobile", "age"]
def __str__(self):
return self.name

My form:

class CheckinPatientMetaForm(ModelForm):
class Meta:
model = customer
exclude = [
'gender',
'maritalstatus',
'occupation',
'bloodgroup'
]

views.py:

def checkin_patient(request):
results = ''
if request.method == 'POST':
form = CheckinPatientMetaForm(request.POST)
print("POST data",request.POST)
else:
form = CheckinPatientMetaForm()
return render(request, 'clinic/checkin.html', {'rnd_num':
randomnumber(), 'form': form, 'SearchResults': results})

Joel G Mathew

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAA%3Diw_9CQJSKzq7EbJnRVBZHbyDF0Nmx7TidBymxJROtY-XfrQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


How can I display an AutoField in a ModelForm?

2018-09-12 Thread Joel Mathew
Question: How can I display an AutoField in a ModelForm. I have a need
where a Form is generated and displayed. Based on the input, I need to
read the AutoField's value and search for it in my database.

My form:

class CheckinPatientMetaForm(ModelForm):
class Meta:
model = customer
exclude = [
'gender',
'maritalstatus',
'occupation',
'bloodgroup'
]

My model:

class customer(models.Model):
# Need autoincrement, unique and primary
cstid = models.AutoField(primary_key=True, unique=True)
name = models.CharField(max_length=35)
age=models.IntegerField()
gender_choices = (('male', 'Male'),
('female', 'Female'))
gender = models.CharField(
choices=gender_choices, max_length=10, default='male')
maritalstatus_choices = ( ('married', 'Married'),
('unmarried', 'Unmarried'))
maritalstatus = models.CharField(
choices=maritalstatus_choices, max_length=10, default='Unmarried')
mobile = models.CharField(max_length=15, default='')
alternate = models.CharField(max_length=15, default='')
email = models.CharField(max_length=50, default='', blank=True)
address = models.CharField(max_length=80, default='', blank=True)
city = models.CharField(max_length=25, default='', blank=True)
occupation = models.CharField(max_length=25, default='', blank=True)
bloodgroup_choices = (('apos', 'A+'),
('aneg', 'A-'),
('bpos', 'B+'),
('bneg', 'B-'),
('opos', 'O+'),
('oneg', 'O-'),
('abpos', 'AB+'),
('abneg', 'AB-'),
('unspecified', '-')
)
bloodgroup = models.CharField(choices=bloodgroup_choices,
max_length=3, default='-', blank=True)
class Meta:
unique_together = ["name", "mobile", "age"]
def __str__(self):
return self.name

views.py:

def checkin_patient(request):
results = ''
if request.method == 'POST':
form = CheckinPatientMetaForm(request.POST)
print("POST data",request.POST)
else:
form = CheckinPatientMetaForm()
return render(request, 'clinic/checkin.html', {'rnd_num':
randomnumber(), 'form': form, 'SearchResults': results})

Relevant part of my template:


{% csrf_token %}


Hospital ID
{{ form.cstid|add_class:"form-control" }}




Name
{{ form.name|add_class:"form-control" }}




Age
{{ form.age|add_class:"form-control" }}


Email
{{ form.email|add_class:"form-control" }}




Mobile
{{ form.mobile|add_class:"form-control" }}

mobile

Alternate
{{ form.alternate|add_class:"form-control" }}




Address
{{ form.address|add_class:"form-control" }}

address



City
{{ form.city|add_class:"form-control" }}







The generated html:





Hospital ID




Name





Age



Email





Mobile


mobile

Alternate





Address


address



City








As you can see, there is not input field for cstid. In the actual
model, it's an automatic field. But in the ModelForm it needs to be a
form input so I can search for a patient's hospitalid (which is this
automatic field). How can I display this field in the form?

Joel G Mathew

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 

Re: How do I make a field optional to enter, in a django ModelForm?

2018-09-12 Thread Ryan Gedwill
There may be a better way, but I’d set ‘required=False’ for the field.

Then on the template where it is actually required, hardcode validation logic 
in either JS or with the template tags, by simply checking if the field is 
empty and displaying an error if it is empty, and submitting the form if it is 
not.

You could also do it the OOP way by inheriting your modelform with a new 
modelform, and overriding the field for the new one.

For example in the regular modelform set required=True, then in the inherited 
version set required=False.

I’d do it the JS/template tag way, and I most likely have done it before.


Sent from my iPhone

> On Sep 12, 2018, at 3:13 PM, Joel Mathew  wrote:
> 
> Scenario:
> I instantiate a ModelForm and pass it to a template which displays the
> form. When POST is submitted, code tries to search the database by any
> of the given inputs. I dont require all inputs to be entered as in the
> Model. I just need one (or more, if user desires to do an AND search)
> to be entered.
> 
> Question: How can I make any of the ModelForm fields optional, where
> in the Model, the field isnt optional. The field isnt optional in the
> Model because I have another ModelForm based on the same Model, where
> user is required to enter all his details.
> 
> My model:
> 
>class customer(models.Model):
># Need autoincrement, unique and primary
>cstid = models.AutoField(primary_key=True, unique=True)
>name = models.CharField(max_length=35)
>age=models.IntegerField()
>gender_choices = (('male', 'Male'),
>('female', 'Female'))
>gender = models.CharField(
>choices=gender_choices, max_length=10, default='male')
>maritalstatus_choices = ( ('married', 'Married'),
>('unmarried', 'Unmarried'))
>maritalstatus = models.CharField(
>choices=maritalstatus_choices, max_length=10, default='Unmarried')
>mobile = models.CharField(max_length=15, default='')
>alternate = models.CharField(max_length=15, default='')
>email = models.CharField(max_length=50, default='', blank=True)
>address = models.CharField(max_length=80, default='', blank=True)
>city = models.CharField(max_length=25, default='', blank=True)
>occupation = models.CharField(max_length=25, default='', blank=True)
>bloodgroup_choices = (('apos', 'A+'),
>('aneg', 'A-'),
>('bpos', 'B+'),
>('bneg', 'B-'),
>('opos', 'O+'),
>('oneg', 'O-'),
>('abpos', 'AB+'),
>('abneg', 'AB-'),
>('unspecified', '-')
>)
>bloodgroup = models.CharField(choices=bloodgroup_choices,
> max_length=3, default='-', blank=True)
>class Meta:
>unique_together = ["name", "mobile", "age"]
>def __str__(self):
>return self.name
> 
> My form:
> 
>class CheckinPatientMetaForm(ModelForm):
>class Meta:
>model = customer
>exclude = [
>'gender',
>'maritalstatus',
>'occupation',
>'bloodgroup'
>]
> 
> views.py:
> 
>def checkin_patient(request):
>results = ''
>if request.method == 'POST':
>form = CheckinPatientMetaForm(request.POST)
>print("POST data",request.POST)
>else:
>form = CheckinPatientMetaForm()
>return render(request, 'clinic/checkin.html', {'rnd_num':
> randomnumber(), 'form': form, 'SearchResults': results})
> 
> Joel G Mathew
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/CAA%3Diw_9CQJSKzq7EbJnRVBZHbyDF0Nmx7TidBymxJROtY-XfrQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/39C71388-474B-4B53-8BA7-D176F6E7B1B6%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


django-filter over models

2018-09-12 Thread René L . Hechavarría
Hello,

I have a model, Jobs and Persons, Jobs are listed in ListView, i need to
add a filter (django-filter), if i filter over field in Jobs all work good,
but i need to filter also for Persons. this is and example

@property
def get_persons(self):
return self.persons_all.all()


That allow to add persons to Jobs model in de html. How i filter over
persons in Jobs view?

Thanks in advance.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEFmArFeu-uprX8C5uOZ7jfbO6pi-oeqYyvb-hjBd5FawMxLvw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.