Re: PostgreSQL get_object_or_404 matching query does not exist when indeed it does?

2017-02-18 Thread Nikoleta Misheva
I missed that it is message.user.username when pasting it here but anyway 
it does not work. I checked it's class and it is str

събота, 18 февруари 2017 г., 21:47:27 UTC+2, Daniel Roseman написа:
>
> On Saturday, 18 February 2017 17:37:49 UTC, Nikoleta Misheva wrote:
>>
>> I am querying PostgreSQL using  *get_object_or_404  *  but it throws and 
>> exception  *PairUsers matching query does not exist  *and after that *During 
>> handling of the above exception, another exception 
>> occurred:django.http.response.Http404: No PairUsers matches the given query 
>> *when indeed I have a record in my db that matches the query and I am 
>> sure I am comparing the right values. How should I fix that?
>> Here is the corresponding code:
>>
>> @channel_session_user
>> def ws_receive(message):
>>  username = message.user
>>  text = json.loads(message['text']).get('text')
>>  # Use my algorithm here
>>  score = score_argument.get_rating(text)
>>  # find the room with our users
>>  print(username)
>>  current_room = get_object_or_404(PairUsers, Q(username_a=username) | 
>> Q(username_b=username))
>>  # current_room = PairUsers.objects.filter(Q(username_a=username) | 
>> Q(username_b=username))
>>
>>  # check which user you got and send the message to the other
>>  if current_room.username_b == username:
>>  current_room.score_b = score
>>  other_channel = Channel(current_room.reply_channel_a)
>>  message.reply_channel.send({'text': text})
>>  other_channel.send({'text': text})
>>  else:
>>  current_room.score_a = score
>>  other_channel = Channel(current_room.reply_channel_b)
>>  message.reply_channel.send({'text': text})
>>  other_channel.send({'text': text})
>>
>>
> Are you sure `message.user` is the same type as  `PairUsers.username_a` or 
> `PairUsers.username_b`? It sounds like one is an instance of User and one 
> is a character username (although it would be weird to use a username 
> there, in which case you should really rename the field).
>
> In any case, you should show your PairUsers model and whatever class 
> `message` is.
> --
> DR.
>

-- 
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/9b5044db-51c1-4991-9877-669256aff910%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Subquery has too many columns

2017-02-18 Thread Melvyn Sopacua
On Saturday 18 February 2017 22:27:46 Elton Pereira wrote:

> class ProdutoServicoManager(models.Manager):
> def get_queryset(self):
> expression = models.Sum(models.F('custounitario__valor') *
> models.F('custounitario__quantidade'))
> total_expr = models.ExpressionWrapper(expression,
> output_field=models.DecimalField())
> return
> super().get_queryset().annotate(custo_producao=total_expr)
> 
> class ProdutoServico(Base):

What's the manager on Base?

> produto = models.BooleanField(default=True)
> descricao = models.TextField()
> objects = ProdutoServicoManager()
> 
> class Meta:
> base_manager_name = 'objects'

Cause this doesn't actually do anything, beside being explicit.

-- 
Melvyn Sopacua

-- 
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/556.fYAOTNoFIl%40devstation.
For more options, visit https://groups.google.com/d/optout.


Subquery has too many columns

2017-02-18 Thread Elton Pereira
When I change the *base_manager_name* and try to update the model an
exception is raised .

This is my code:

class ProdutoServicoManager(models.Manager):
def get_queryset(self):
expression = models.Sum(models.F('custounitario__valor') *
models.F('custounitario__quantidade'))
total_expr = models.ExpressionWrapper(expression,
output_field=models.DecimalField())
return super().get_queryset().annotate(custo_producao=total_expr)

class ProdutoServico(Base):
produto = models.BooleanField(default=True)
descricao = models.TextField()
objects = ProdutoServicoManager()

class Meta:
base_manager_name = 'objects'

I thought I had found the answer to this problem in issue 19513
, but it was already closed.
Apparently this error reappears when I try to annotate the base_manager.

Should I open a new issue?
-- 

Elton Pereira de Lima

-

Quem nada dúvida, nada descobre.

-- 
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/CAOxxup03o_ie%2BusbxSMcNozAf8hAiuOy8oaX1F8%3DjP8AoaFBYQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: PostgreSQL get_object_or_404 matching query does not exist when indeed it does?

2017-02-18 Thread Daniel Roseman
On Saturday, 18 February 2017 17:37:49 UTC, Nikoleta Misheva wrote:
>
> I am querying PostgreSQL using  *get_object_or_404  *  but it throws and 
> exception  *PairUsers matching query does not exist  *and after that *During 
> handling of the above exception, another exception 
> occurred:django.http.response.Http404: No PairUsers matches the given query 
> *when indeed I have a record in my db that matches the query and I am 
> sure I am comparing the right values. How should I fix that?
> Here is the corresponding code:
>
> @channel_session_user
> def ws_receive(message):
>  username = message.user
>  text = json.loads(message['text']).get('text')
>  # Use my algorithm here
>  score = score_argument.get_rating(text)
>  # find the room with our users
>  print(username)
>  current_room = get_object_or_404(PairUsers, Q(username_a=username) | 
> Q(username_b=username))
>  # current_room = PairUsers.objects.filter(Q(username_a=username) | 
> Q(username_b=username))
>
>  # check which user you got and send the message to the other
>  if current_room.username_b == username:
>  current_room.score_b = score
>  other_channel = Channel(current_room.reply_channel_a)
>  message.reply_channel.send({'text': text})
>  other_channel.send({'text': text})
>  else:
>  current_room.score_a = score
>  other_channel = Channel(current_room.reply_channel_b)
>  message.reply_channel.send({'text': text})
>  other_channel.send({'text': text})
>
>
Are you sure `message.user` is the same type as  `PairUsers.username_a` or 
`PairUsers.username_b`? It sounds like one is an instance of User and one 
is a character username (although it would be weird to use a username 
there, in which case you should really rename the field).

In any case, you should show your PairUsers model and whatever class 
`message` is.
--
DR.

-- 
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/9aeb5a31-daf9-4f53-aff6-bfbd1c909c91%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Django forms.CharField subclass with additional attribute

2017-02-18 Thread Sandeep Raheja
I am trying to generate a form dynamically and want to assign indentation 
of form fields. I am trying to assign an custom attribute offset to 
forms.CharField in subclass. I plan to use this logic to create a form 
dynamically from an xml file, where the fields would be indented based on 
the depth of the node. 

I am unable to retrieve the value of offset while rendering the template 
and hence unable to assign the margin-left style parameter. The final html 
output is also shown. 



My forms.py file :




class MyCharField(forms.CharField):
def __init__(self, *args, **kwargs):
self.offset = kwargs.pop('offset', 0)
super(MyCharField, self).__init__(*args, **kwargs)


class MyDynamicForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MyDynamicForm, self).__init__(*args, **kwargs)
self.fields["Field_A"] = MyCharField(label="Input A", offset="5"
)
self.fields["Offset_Field_B"] = MyCharField(label="Input B", 
offset="50")




My Views.py looks like this:


class MyDynamicView(View):
template_name = 'demo/myform.html'
form_class = MyDynamicForm

def get(self, request, *args, **kwargs):
form = self.form_class()
return render(request, self.template_name, {'form': form})




My template file using bootstrap looks like this:

   
 {% extends 'demo/base.html' %}
{% load bootstrap3 %}
{% block content %}

{% csrf_token %}
{% for field in form %}

{{ field.label}}




{% endfor %}  
{% buttons submit='OK' reset='Cancel' layout='horizontal' %}{% 
endbuttons %}

{% endblock %}




The html output is:






Input A






Input B






 OK Cancel


-- 
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/9ca3642f-c606-403b-80e3-113ebd8d686e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Subclassing forms.CharField with additional attribute

2017-02-18 Thread Sandeep Raheja
I am trying to generate a form dynamically and want to assign indentation 
of form fields. I am trying to assign an custom attribute offset
to forms.CharField in subclass. I plan to use this logic to create a form 
dynamically from an xml file, where the fields would be indented based on 
the depth of the node. 

I am unable to retrieve the value of offset while rendering the template 
and hence unable to assign the margin-left style parameter. The final html 
output is also shown. 



My forms.py file :



   
 class MyCharField(forms.CharField):
def __init__(self, *args, **kwargs):
self.offset = kwargs.pop('offset', 0)
super(MyCharField, self).__init__(*args, **kwargs)





class MyDynamicForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MyDynamicForm, self).__init__(*args, **kwargs)
self.fields["Field_A"] = MyCharField(label="Input A", offset="5"
)
self.fields["Offset_Field_B"] = MyCharField(label="Input B", 
offset="50")




My Views.py looks like this:


class MyDynamicView(View):
template_name = 'demo/myform.html'
form_class = MyDynamicForm

def get(self, request, *args, **kwargs):
form = self.form_class()
return render(request, self.template_name, {'form': form})




My template file using bootstrap looks like this:


{% extends 'demo/base.html' %}
{% load bootstrap3 %}
{% block content %}

{% csrf_token %}
{% for field in form %}

{{ field.label}}




{% endfor %}  
{% buttons submit='OK' reset='Cancel' layout='horizontal' %}{% 
endbuttons %}

{% endblock %}




The html output is:







Input A






Input B






 

OK Cancel







-- 
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/2e729d73-205b-454f-a202-27e337fb8baf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


PostgreSQL get_object_or_404 matching query does not exist when indeed it does?

2017-02-18 Thread Nikoleta Misheva
I am querying PostgreSQL using  *get_object_or_404  *  but it throws and 
exception  *PairUsers matching query does not exist  *and after that *During 
handling of the above exception, another exception 
occurred:django.http.response.Http404: No PairUsers matches the given query 
*when indeed I have a record in my db that matches the query and I am sure 
I am comparing the right values. How should I fix that?
Here is the corresponding code:

@channel_session_user
def ws_receive(message):
 username = message.user
 text = json.loads(message['text']).get('text')
 # Use my algorithm here
 score = score_argument.get_rating(text)
 # find the room with our users
 print(username)
 current_room = get_object_or_404(PairUsers, Q(username_a=username) | 
Q(username_b=username))
 # current_room = PairUsers.objects.filter(Q(username_a=username) | 
Q(username_b=username))

 # check which user you got and send the message to the other
 if current_room.username_b == username:
 current_room.score_b = score
 other_channel = Channel(current_room.reply_channel_a)
 message.reply_channel.send({'text': text})
 other_channel.send({'text': text})
 else:
 current_room.score_a = score
 other_channel = Channel(current_room.reply_channel_b)
 message.reply_channel.send({'text': text})
 other_channel.send({'text': text})

-- 
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/1c2a1700-0759-426d-940c-3dcfa8c2b4c1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: file fields

2017-02-18 Thread ايهاب توفيق
thank you for your help I solve the problem and it working

بتاريخ الخميس، 26 يناير، 2017 1:04:28 ص UTC+2، كتب ايهاب توفيق:
>
> I am new in django and to know how to use multiple file field in the same 
> model can any one help me
>

-- 
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/7bcfd585-79a5-4297-8cd2-0557ff3238de%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


short_description not working

2017-02-18 Thread Qikai Gu
Hi folks,

I'm following the first django app tutorial (polls) and I'm stuck at part 7 
:
 
the `short_description` doesn't work.

The short_description I have provided 'Published recently?' in models.py 
did not reflect on admin app (/admin/polls/question/) as shown in tutorial 
(need to show 'PUBLISHED RECENTLY?', but still shown "WAS PUBLISHED 
RECENTLY")?! There is no any error shown.

Does I missed something or behaviors of short_description is somehow 
changed? 
BTW: Reseting application and model did not help. 

I'm using Django 1.10.5 with Python 3.5.2 in Ubuntu 16.04. 

Thank You 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/a8200761-67f7-48d1-819f-d53292c95900%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


How to get list of all record on basis of year and month

2017-02-18 Thread ankur
Hello,

I am new in django. I would like to display the list of all record of 
particular month and year.Please find below model structure of my table.

class Order_Detail(models.Model):
  
  
Order_Date_Time=models.DateTimeField(default=datetime.datetime.now(),blank=True,null=True)

and 
for example i would like to query record of month: january and year : 2016
Query am trying is:
  
month_wise=Order_Detail.objects.filter(Order_Date_Time__year=2016,Order_Date_Time__month=1)

am getting empty list.


-- 
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/521e79df-244b-4c8c-bec4-6f48e0361211%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: set initial value for field when a model insert page loaded in django admin

2017-02-18 Thread Derek
I have cut and trimmed this from of some of my own code, so may not be 100%:

class MyModelForm(ModelForm):

class Meta:
model = MyModel

def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
self.fields['my_field'].initial= 'some_value'


class MyModelAdmin(ModelAdmin):
form = MyModelForm



On Saturday, 18 February 2017 08:35:54 UTC+2, sjsade...@gmail.com wrote:
>
> hi, i want set initial value for field when a model insert page loaded in 
> django admin.
> i think i can do this work with get_field_queryset method but in django 
> there is no toturial for get_field_queryset.
> how to do this work?
> thanks ...
>

-- 
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/d76d8743-8970-477b-bea2-0a7c8ba2ffd3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Add new primary key to model

2017-02-18 Thread Joakim Hove
Hello;

I have a model with a CharField which is used as primary key:

class MyModel( Model ):
 string_id = CharField("StringID" , primary_key = True )

I would now like to migrate this model to have a normal integer AutoField 
as primary key, and the string_id field should just be a unique string:


class MyModel( Model ):
   int_id = AutoField( primary_key = True )
   string_id = CharField( "StringID", unique = True )


I have tried several ways of doing this, but it either fails in the 
'makemigrations' step, or when I try to apply the migration. Any tips?

Joakim

-- 
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/338750fb-7b7c-444c-a920-50b018e2a3a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Question about m2m and forms

2017-02-18 Thread Carlo Ascani


Il giorno venerdì 17 febbraio 2017 20:09:15 UTC+1, Matthew Pava ha scritto:
>
> Hi Carlos, 
> You probably want to create a new widget and override its 
> label_from_instance method. 
>
> class BModelMultipleChoiceField(forms.ModelMultipleChoiceField): 
> def label_from_instance(self, obj): 
> return "%s (%s)" % (obj, obj.count_a) 
>
>
> And then you'll want to change your queryset to annotate the count so you 
> can access it in the new field. 
> class AFilterForm(forms.Form): 
> to_b = BModelMultipleChoiceField( 
> widget=forms.CheckboxSelectMultiple(), 
> queryset = 
> B.objects.all().annotate(count_a=Count('a_set'))) 
>
>
 Thank you!
That works really well.


-- 
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/c023ebf0-aeb7-4932-8e6a-b184a63c5b65%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.