Problem with submitting form in my django project! Urgent help required

2015-02-17 Thread Ketan Bhatt


I am facing a problem with my project, I posted about it on Stack Overflow, 
here is the link: 
http://stackoverflow.com/questions/28539441/reference-objects-using-foreign-keys-in-django-forms


Here is m code on github, I am currently working on the feature_forms 
branch: https://github.com/ketanbhatt/sketch-a-wish/tree/feature_forms


I will summarize my problem again, I am building a site in which users can 
login and make a wish. Other users can first a get a wish to sketch, and 
then submit the sketch they made for the assigned wish.

I have been able to create all the views and forms, except for the 
submitting sketch view. I want:

1. Only the wishes that has been assigned to the logged in user to be 
displayed in the Submit_sketch form

2. User selects the wish he wants to upload the sketch for, and submits.


Please help!

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ee013223-1beb-4a1b-80a7-3f72867724d2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Query and order by time difference of a specified time.

2016-05-23 Thread Ketan Bhatt
Take a look at the `extra` method of the queryset in Django. It allows you to 
do what you are trying to do by raw SQL. 

https://docs.djangoproject.com/en/1.9/ref/models/querysets/#django.db.models.query.QuerySet.extra

Check the above link, the example with `annotate` will be interesting for you. 

I think your aim should be to create a new field and then do a `order_by` and 
`first`. 

-- 
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/5b556c8b-2012-4bd7-89c5-bddbbdb4c3b9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: get all columns as a list

2016-05-23 Thread Ketan Bhatt
Can you not do something like

`qs.filter(...info that is coming in...).exists()` 
If ^ is True, then update it, otherwise create a new object? 

-- 
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/d9ffaeb7-f796-4d74-8ab3-1ee0cf1f5ca6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to aggregate on insert?

2016-05-23 Thread Ketan Bhatt
Hey Erik,

What Django version are you on?

On Tuesday, May 24, 2016 at 1:28:57 AM UTC+5:30, Erik Cederstrand wrote:
>
> Hi, 
>
> I have inherited a legacy Item model that has a composite unique key 
> consisting of a Customer ID and a per-customer, incrementing Item ID. 
> Assume I can't change the model. 
>
> On inserts, the legacy code would let the database increment the Item ID 
> to avoid race conditions. Something like this: 
>
>INSERT INTO item_table (customer_id, item_id, name, ...) VALUES (123, 
> (SELECT MAX(item_id) FROM item_table WHERE customer_id =123) + 1, 'MyItem', 
> ...); 
>
>
> Is there any way I can do the same using the Django ORM without opening up 
> for race conditions? I.e. something better than: 
>
>i = Item(customer_id=123, name='MyItem', ...) 
>i.item_id = 
> Item.objects.filter(customer_id=123).aggregate(Max('item_id'))['item_id__max']
>  
> + 1 
>i.save() 
>
> Or do I just wrap that in a loop and catch IntegrityError if I don't want 
> to use raw SQL? 
>
>
> Thanks, 
> Erik

-- 
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/cb1f1135-416b-44b0-8b2c-c4415ff47e69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Beginner

2016-05-26 Thread Ketan Bhatt
The official tutorial is difficult for a beginner, it was for me.

I would suggest you start with Django girls, or "Tango with Django"

On Thu, May 26, 2016, 5:52 PM Jani Tiainen  wrote:

> And after that official tutorial there exists Django Girls and Django
> Taskbuster which are much more verbose tutorials.
>
>
> On 26.05.2016 14:45, Matthias Müller wrote:
>
> h, what about making the tutorial ?
>
> https://docs.djangoproject.com/en/1.9/intro/tutorial01/
>
> enjoy it !
>
>
>
> 2016-05-26 9:16 GMT+02:00 Atchuta Saikumar :
>
>> Hello ...
>> I want to learn Django.I have some basic knowledge in python but don't
>> know oop concepts in python.what to do now?
>> --
>> 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/44bad709-1c35-40ec-a154-1792a2cf3b14%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/CAA2xsHSZG91o-MVwvYZ27FO_xAND6QVYpxPYYCCxU4u-7HkmzQ%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/5746EA71.3090600%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/CACyRkBWu4w4%2B3E%2BkmOwFAtwpTDvjhn9-%3DUcUs9%2By18%3DThDPdbw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to select undetermined field(s) in django model?

2016-05-26 Thread Ketan Bhatt
It just means the "field_name" you are using in the filter query doesn't
exist.

Multiple reasons:
1. The model doesn't really have that field in its definition.
2. You forgot migrating your database.
3. The field name being sent from your form is not right.

To get a clearer picture, try the same filter query directly on Django
shell ("Python manage.py shell") and check what happens.

On Thu, May 26, 2016, 5:54 PM Jianshu Hu  wrote:

> To all,
>
> I am new to django, and learning to establish a site.
>
> Here is my problem:
> I have got a databse and a table in mysql, and am using django to read
> the data from it.
> I got a pair of values [FIELD_NAME,VALUE] from the GET method of a
> html page. When the values passed to the searching function, I want to get
> the searching result.
> I knew how to do this in MySQLdb, but unable to achieve it in django
> model.
> In MySQLdb:
> cur.execute("SELECT COLUMN_A,COLUMN_B,COLUMN_C FROM mydatabase
> WHERE %s=%s",(filed_name,value))
> However, in django:
> results=mydatabase.objects.filter(filed_name=value)
> I got an error: Cannot resolve keyword 'filed1' into field. Choices
> are: Bla bla bla
>
>What should I do?
>
> Bests.
>
>
>
> --
> 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/bddb8755-33d2-4bb4-9769-3fb350a43ff0%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/CACyRkBUFVBe2K5Ym4xokFdGdNvhFfN-8%3D54YFSdf_NEm_f_Ssw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to resize and store pics with Django

2016-06-03 Thread Ketan Bhatt
This is how you can compress the images while saving:

```python

import urllib2
import cStringIO

from boto3.session import Session

from PIL import Image


aws_session = Session(
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
region_name=settings.AWS_DEFAULT_LAMBDA_REGION
)
s3_resource = aws_session.resource('s3')

im = Image.open(cStringIO.StringIO(urllib2.urlopen(image_url).read()))

w, h = im.size
w *= 0.5
h *= 0.5

im.thumbnail((w, h))
buf = cStringIO.StringIO()
im.save(buf, format="JPEG")

s3_resource.Object(bucket, filename).put(Body=buf.getvalue(), ACL='public-read')

```


I am reading images from a URL and compressing them and then saving to
S3 using boto.


See how and where you want to store your images and just change the
required line.


On Fri, Jun 3, 2016 at 10:25 PM ivan77  wrote:

> HI,
>
> I would like my Django App to have the functionality where a person can
> upload pictures (that would be resized).
>
> Can you please give me any advice on the best way to do that in Django.  I
> will be using a postgres DB for the app.
>
> Also, if there are any Python/Django apps that you can suggest, please let
> me know.
>
> I found this on stack overflow which is some generic advice:
>
> http://stackoverflow.com/a/561475/3856624
>
>
> Store the pictures on the file system and picture locations in the
> database.
>
> Why? Because...
>
>1. You will be able to serve the pictures as static files.
>2. No database access or application code will be required to fetch
>the pictures.
>3. The images could be served from a different server to improve
>performance.
>4. It will reduce database bottleneck.
>5. The database ultimately stores its data on the file system.
>6. Images can be easily cached when stored on the file system.
>7.
>
>
> Thanks for any ideas,
>
> Ivan
>
>
> --
> 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/530227c6-e9ad-4163-a15d-5afa9eab880a%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/CACyRkBWCg%2BQdF2L_5mrXwxBRqzv_hCXNo73hu4sBt_XmW-kacg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to resize and store pics with Django

2016-06-03 Thread Ketan Bhatt
I am storing links of these images as I need to show them on the frontend,
not connected to any user.

But I have done what you are talking about too.
So Django gives you a Model File Field. Add that to your Model. That will
allow you to save and store files. While saving a model object, just use
the above code to compress the images.

You can find about FileField and how to use it from here
https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.FileField



On Fri, Jun 3, 2016 at 10:53 PM ivan77  wrote:

> Hi Ketan,
>
> Thanks for the information!
>
> How is it that you keep the images associated with other tables in the
> database?
>
> What I am specifically thinking about is if you want to associate specific
> images with specific users who uploaded, for example?  Some kind of bridge
> (many to many table)?
>
> Ivan
>
> --
> 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/bc43f2b0-9527-407b-b66f-774ad97d565c%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/CACyRkBXgUQUpHy1LearXu7qNhRBcxAB47ca24jO4P7sKq%3DVLOA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


TransactionManagementError from inside atomic block

2017-03-25 Thread Ketan Bhatt
I have a method that updates a row in the table.
To avoid race condition, I locked the row using `select_for_update` inside
an `atomic` block and doing the update.
This method is called from a celery task.

This works well on my local machine. But when this gets called on my
production server (two tasks being picked up by two workers at the same
time and therefore trying to access the same row at once), I get:
`TransactionManagementError('select_for_update cannot be used outside of a
transaction.',)`

I checked that to use `select_for_update`, autocommit should be False.
Inside the atomic block `get_autocommit` returns False.

Now on production this must be returning `TRUE`.


What could be the reason?

-- 
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/CACyRkBU8cpRgm8omT5PnkdW0-uxPF9U87TqMzm16kWmdfqkNrw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: TransactionManagementError from inside atomic block

2017-03-29 Thread Ketan Bhatt
Hey Simon,

Thank you for your response, we were able to resolve the problem soon after
I posted the query.

And yes, you are correct, it was an issue with having multiple DB
connections and the `auto-commit` being turned off for one, but then
another connection being used for querying.

We earlier had a master-slave configuration for our DB and were using a DB
router to route all reads to the slave. That is still in use and that is
why the `deafualt` connection was used as we entered the `atomic` block,
but `slave` when we made the read.

Thank you again.

On Thu, Mar 30, 2017 at 4:39 AM Simon Charette  wrote:

> Hi Ketan,
>
> I'm afraid this will be really hard to solve without the code that is
> raising this exception.
>
> Some details that could help debug the issue:
>
> 1. Are your workers multi-threaded?
> 2. Are you sure you are not calling functions performing manual
> transaction management?
> 3. Is your atomic() block using the same database involved in the
> select_for_update() call?
>
> e.g. Are you using transaction.atomic() (which will use the default
> database)
> but have database routers that could route reads/writes to another db?
>
> Cheers,
> Simon
>
> Le samedi 25 mars 2017 15:08:19 UTC-4, Ketan Bhatt a écrit :
>
> I have a method that updates a row in the table.
> To avoid race condition, I locked the row using `select_for_update` inside
> an `atomic` block and doing the update.
> This method is called from a celery task.
>
> This works well on my local machine. But when this gets called on my
> production server (two tasks being picked up by two workers at the same
> time and therefore trying to access the same row at once), I get:
> `TransactionManagementError('select_for_update cannot be used outside of
> a transaction.',)`
>
> I checked that to use `select_for_update`, autocommit should be False.
> Inside the atomic block `get_autocommit` returns False.
>
> Now on production this must be returning `TRUE`.
>
>
> What could be the reason?
>
> --
> 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/91f6b088-e255-4ff9-a9c5-f033ffdf4211%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/91f6b088-e255-4ff9-a9c5-f033ffdf4211%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> 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/CACyRkBVpKO1Khuf4Z%3D_PggA3TPcFa92anCgC_p_tzw92kwbQVw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.