Re: OnChange or OnBlur event in javascript to run python function in Django and return the results???

2012-12-12 Thread Mario Gudelj
Hey,

First you need to add some id to that field and then use a jQuery selector
to get the value from it on blur or input:

$('#some_id).on("blur", function(){
   //make your ajax call here
});

To make the Ajax call you can use jQuery's .post() or .get(). I guess post
method would make a bit more sense in this case. Look up
http://api.jquery.com/jQuery.post/

So your JS looks something like this now:

$('#some_id).on("blur", function(){
$.post('your-url/', function(data) {
   $('.result').html(data); // do whatever you want with the
returned data
 });
});

You need to know how you want to process your data in JS, because you can
return a string or JSON.

At Django's end you need to create your url in the view and create a view
that captures that post.

Your view could look like this:

@csrf_exempt
def your_view(request):
if request.is_ajax() and request.method == 'POST':
captured_post_parameter = request.POST.get('you_field_name')
# blah blah - do your logic here
# you can return either HttpResponse(response) or use simplejson to
return json response

That's really all there is to it. I hope it helps!

-mario
www.zenbookings.com




On 13 December 2012 14:06, Murtaza  wrote:

> Hello All,
>
> I have developed an internal tool with Django and Python in the backend,
> now it seems like I have hit a road block.
>
> I am very new to Javascript. And the site has to be very dynamic and it
> needs to be able to do something like the following:
>
> In the input box,
>
>   1. The user has entered data
>
>   2. When the user moves out from the box, it runs an onBlur or onChange
> or some event function which calls a python code that takes some arguments,
> and takes other data from the page and Inserts that data in the database
> and saves.
>
>   3. It does that with out changing the url/refreshing the page or
> anything just staying on the same page.
>
> Any ideas how to accomplish this with Python and Django.
>
> Thanks, any help is appreciated.
> Murtaza Pitalwala
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/rTK5hli6aJIJ.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Datastructure using django.

2012-12-12 Thread b1-


On Monday, December 10, 2012 8:15:41 PM UTC+2, Subhodip Biswas wrote:
>
> Hi all, 
>
> I am trying to implement a tree structure using django. There are two 
> packages I am looking at mptt and treebeard. Treebeard seems easy. 
>
> I have however two questions:
>
> 1. With mptt I found that if you have moved one child to another parent 
> both db and instance is updated but you need to reload or reregister every 
> time you call such action.Is there a better way of doing this?
>

This is normal for django because for site prefarable structure with faster 
reading than faster writing.

2. If my child have more than one parent, the structure does not remain a 
> tree anymore. It becomes a graph i guess, How do you handle such situation. 
>

mptt has 'TreeManyToManyField' so I think this is possible
 

> 3. How do I add treebeard or mptt into django admin. If I have one field 
> name and I register it in admin.py. I get error regarding depth,path, 
> numchild and name should be present. How do I handle this situation.
>

For me this works

# admin.py
from django.contrib import admin
admin.site.register(ModelName)
 

Can you post your code and errors

>
> Apologies for asking so many questions.
>
>
> -
> Regards
> Subhodip Biswas
>
>
> GPG key : FAEA34AB
> Server : pgp.mit.edu
> http://subhodipbiswas.wordpress.com
> http:/www.fedoraproject.org/wiki/SubhodipBiswas
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/0iUToHxNFLQJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: JSONResponseMixin: Best handle "context" before dumping it to JSON?

2012-12-12 Thread Micky Hulse
Hi Russ! Wow, thanks so much for the quick reply and additional details! :)

On Wed, Dec 12, 2012 at 7:12 PM, Russell Keith-Magee
 wrote:
> Can you use a try-catch to flag encoding problems? Sure - that's a
> completely reasonable solution.
> Is a blanket try-catch a bad idea? Yes, because it hides all sins. The
> exceptions you're expecting are quite predictable -- if dumps() can't encode
> the object, you're going to get a TypeError. Therefore it makes sense to
> catch the TypeError, and provide a response that indicates that encoding was
> the problem. If you catch *all* exceptions, and just assume that encoding
> was the problem, then the "once in a million that happens 9 times out of 10"
> will be some other error generated by the serialisation of the object that
> *isn't* a type encoding error (an attribute error in your serializer, for
> example). If you're masking those errors, they're much harder to find.

Awesome! That makes total sense!

For now, I think I'll add a try/catch TypeError check and then test
thoroughly for corner cases to see if I need further data-type
handling.

Thanks again for the pro help an assistance! I owe you one! ;)

Have a nice night.

Cheers,
Micky

-- 
Micky Hulse
Web Content Editor
The Register-Guard
3500 Chad Drive
Eugene, OR 97408
Phone: (541) 338-2621
Fax: (541) 683-7631
Web: 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: JSONResponseMixin: Best handle "context" before dumping it to JSON?

2012-12-12 Thread Russell Keith-Magee
On Thu, Dec 13, 2012 at 11:03 AM, Micky Hulse  wrote:

> Hello Russ! Thank you so much for your pro help and informative reply.
> I really appreciate it. :)
>
> No worries at all. Glad I could be of assistance.


> I've been wondering if I could just add a simple try/catch, like this code
> here:
>
> <
> https://github.com/registerguard/django-ad-manager/blob/57f77dd0b5b59351ad50629f72c9fbb2083b1dd7/ad_manager/decorators.py#L37-L44
> >
>
> 
>
> try:
> data = simplejson.dumps(objects)
> except:
> data = simplejson.dumps(str(objects))
>
> 
>
> Note where str() is being used... Though, my co-worker has told me
> that using "except" without an argument is bad style and frowned upon.


Can you use a try-catch to flag encoding problems? Sure - that's a
completely reasonable solution.

Is a blanket try-catch a bad idea? Yes, because it hides all sins. The
exceptions you're expecting are quite predictable -- if dumps() can't
encode the object, you're going to get a TypeError. Therefore it makes
sense to catch the TypeError, and provide a response that indicates that
encoding was the problem. If you catch *all* exceptions, and just assume
that encoding was the problem, then the "once in a million that happens 9
times out of 10" will be some other error generated by the serialisation of
the object that *isn't* a type encoding error (an attribute error in your
serializer, for example). If you're masking those errors, they're much
harder to find.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



OnChange or OnBlur event in javascript to run python function in Django and return the results???

2012-12-12 Thread Murtaza
Hello All,

I have developed an internal tool with Django and Python in the backend, 
now it seems like I have hit a road block. 

I am very new to Javascript. And the site has to be very dynamic and it 
needs to be able to do something like the following:

In the input box, 

  1. The user has entered data 

  2. When the user moves out from the box, it runs an onBlur or onChange or 
some event function which calls a python code that takes some arguments, 
and takes other data from the page and Inserts that data in the database 
and saves. 

  3. It does that with out changing the url/refreshing the page or anything 
just staying on the same page.

Any ideas how to accomplish this with Python and Django. 

Thanks, any help is appreciated.
Murtaza Pitalwala


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/rTK5hli6aJIJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Problem at activating the admin site (Django tutorial part 2)

2012-12-12 Thread mdr9572
Same for me as well with a FreeBSD system and normal username, 
LANGUAGE_CODE = 'en_us', database is postgresql with UTF-8 encoding. I went 
through the steps in tutorial_02 to set up the admin site, and got the same 
error as pasztilla. The django_site table in my database is empty.

We are evaluating django for use in a major project, but if the tutorial 
does not work, then we'll probably just pass on it and go with a PHP-based 
system.


On Friday, May 18, 2012 4:58:50 AM UTC-4, Ravi Shakya wrote:
>
> I am also facing the exact problem indicated by pasztilla.
> My usernaleme thing is fine (normal) but I have not been able to fix this 
> issue.
> Any concrete cause/solution to this?
>
> On Friday, August 26, 2011 3:35:39 AM UTC+5:45, pasztilla wrote:
>>
>> Hi Babatunde, 
>>
>> Hi Babatunde, 
>>
>>
>>
>> SITE_ID == 1 
>>
>>
>>
>> then I did what you've suggested:(see the result) 
>>
>>
>>
>> Microsoft Windows [verziószám: 6.1.7601] 
>>
>> Copyright (c) 2009 Microsoft Corporation. 
>> Minden jog fenntartva. 
>>
>>
>>
>> C:\Users\Pászkán Attila> 
>>
>>
>> D:\>cd Atilla\Programoz\Django\Gyakorlaas\mysite 
>>
>>
>> D:\Atilla\Programoz\Django\Gyakorlaas\mysite>python manage.py shell 
>>
>>
>> Python 2.7.1 (r271:86832, Feb  7 2011, 11:30:38)[MSC v.1500 32 bit 
>> (Intel)] on win32 
>>
>> Type "help", "copyright", "credits" or "license" for more information. 
>> (InteractiveConsole) 
>>
>>
>> >>> from django.contrib.sites.models import Site 
>>
>> >>> Site.objects.get(id=1) 
>>
>>
>> Traceback (most recent call last): 
>>
>>   File "", line 1, in  
>>
>>   File "D:\Python27\lib\site-packages\django\db\models\manager.py", 
>> line 132, in get 
>>
>> return self.get_query_set().get(*args, **kwargs) 
>>
>>   File "D:\Python27\lib\site-packages\django\db\models\query.py", line 
>> 349, in get 
>>
>> % self.model._meta.object_name) 
>>
>> DoesNotExist: Site matching query does not exist. 
>>
>>
>> >>> 
>>
>>
>>
>>
>>
>>  +  
>>
>> Meanwhile came an idea that this error could have slyly, 
>> somehow undercover, something to do with my User Name (under Windows) 
>> which contains some unusual characters (two á-s, see above) ... 
>> I've set up a new user (without unusual characters) 
>> and repeated the whole procedure (building up an application 
>> according to the tutorial) and it worked fine!!! :-) 
>>
>>
>> So, seemingly the problem has been resolved! :-) 
>> (It would be interesting to find out why and how 
>> has influenced that unusual user name the functioning 
>> of my admin-site...but it is maybee to much effort to investigate) 
>>
>> Anyway,  thanks for your efforts and support! 
>>
>> PA 
>>
>>
>> On aug. 25, 19:28, Babatunde Akinyanmi  wrote: 
>> > Hi paszkan, 
>> > First I want you to go to your settings file and tell me what value is 
>> > specified for SITE_ID. You will need that value for the next step. 
>> > 
>> > In your terminal where you would have typed "python manage.py 
>> > runserver" type "python manage.py shell" 
>> > 
>> > Doing that would load django's inbuilt shell. 
>> > 
>> > Next type: 
>> > "from django.contrib.sites.models import Site" 
>> > If there's no error type this is where you would need the SITE_ID 
>> > setting value. Assuming SITE_ID value is 1 then type: 
>> > "Site.objects.get(id=1)" 
>> > 
>> > Do this and give feed back 
>> > 
>> > On 8/25/11, Pászkán Attila  wrote: 
>> > 
>> > 
>> > 
>> > > Can you give more details? 
>> > 
>> > > 2011/8/25 Babatunde Akinyanmi  
>> > 
>> > >> The error you stated normally happens when there are problems with 
>> the 
>> > >> django_site table created when you start a project. 
>> > 
>> > >> On 8/25/11, Babatunde Akinyanmi  wrote: 
>> > >> > Please say exactly what you did. It will be easier to help that 
>> way 
>> > 
>> > >> > On 8/25/11, raj  wrote: 
>> > >> >> In ur urls.py, 
>> > >> >> Did u import admin, make it discoverable, and also uncomment the 
>> / 
>> > >> >> admin/ url? 
>> > >> >> Also, make sure that you syncdb by running python manage.py 
>> syncdb. 
>> > >> >> Lemme know if that helps :) 
>> > >> >> On Aug 25, 9:29 am, pasztilla  wrote: 
>> > >> >>> Hi there, 
>> > >> >>> I'm just trying to accomodate myself with Django - and so I 
>> tried to 
>> > >> >>> go through the Django Tutorial from the Django official site. 
>> There 
>> > >> >>> went everything ok with part 1,  but at the very beginning of 
>> the part 
>> > >> >>> 2 I've met a problem(
>> https://docs.djangoproject.com/en/1.3/intro/ 
>> > >> >>> tutorial02). In spite of that I've perform all settings/changes 
>> > >> >>> described in the 'Activate the admin site' section going to the 
>> next 
>> > >> >>> section 'Start the development server' (ie. 
>> 127.0.0.1:8000/admin/) led 
>> > >> >>> merely to an error message: 
>> > 
>> > >> >>> "DoesNotExist at /admin/ 
>> > >> >>> Site matching query does not exist. 
>> > >> >>> Request Method: 
>> > >> >>> GET 
>> > >> >>> Request URL:http://127.0.0.1:8000/admin/ 
>> > >> >>> Django Version:

Re: JSONResponseMixin: Best handle "context" before dumping it to JSON?

2012-12-12 Thread Micky Hulse
Hello Russ! Thank you so much for your pro help and informative reply.
I really appreciate it. :)

Sorry for my late reply... A few non-Django projects jumped in the way
of me tending to my list e-mails.

See my replies, inline, below:

On Tue, Dec 11, 2012 at 3:51 PM, Russell Keith-Magee
 wrote:
> It's a little hard to provide an example here without knowing exactly what
> is being serialised -- that's why the example in the docs is deliberately
> naive.
> json.dumps() will handle almost all the datatypes that come out of the box
> with Python. However, as soon as you have non-default types -- querysets,
> models, or anything else -- the default handling falls over. Exactly what
> handling is required depends on what non-default types exist in the content
> you want to serialize.

Ah, I see. Now that you point it out, I understand now why the docs
left out those details.

Not sure if this will help, but here's the code that creates the data
I end up dumping to JSON:



(Note to future readers: those lines may move in a few days as I tweak
the code.)

Basically, I'm just getting string values from my table and not doing
anything special and/or handling any non-default types.

And just for completeness' sake, the JSON mixin is here:



... specifically, this line:



(IBID)

... it's pretty much straight from the docs.

> Error handling really doesn't require anything more than catching the
> exception if the encoder fails.

I've been wondering if I could just add a simple try/catch, like this code here:





try:
data = simplejson.dumps(objects)
except:
data = simplejson.dumps(str(objects))



Note where str() is being used... Though, my co-worker has told me
that using "except" without an argument is bad style and frowned upon.

I'm sure once I've read through the links you've provided, I'll be on
the right track... The above try/catch solution seems a little too
simple.

> The solution will usually come in the form of a custom encoder class.
> Python's docs provide an example of how this can be done [1]; obviously,
> you'll need to adapt this to whatever data types you need to serialize.
> The other option is to use Django's serializers [2]; this mechanism includes
> built-in support for serialising querysets and objects, which removes the
> need for a lot of special handling.

Ah, great info! Thank you for pointing me in the right direction. I
plan on reading through these docs tonight.

Much appreciated and have a great night!

Cheers,
Micky

-- 
Micky Hulse
Web Content Editor
The Register-Guard
3500 Chad Drive
Eugene, OR 97408
Phone: (541) 338-2621
Fax: (541) 683-7631
Web: 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Question on Multiple Database Connections

2012-12-12 Thread Matthew Woodward
I'm working on an application that uses two databases -- one is the main
database that Django ORM talks to, where sessions are stored, etc. and the
other is an external database where we need to run some storedprocs to
retrieve some data.

This is all working fine for the most part but I ran into a situation today
that's causing some odd behavior so I figured I'd seek clarification. (This
is SQL Server using django-pyodbc in case that matters.)

I have the functions that talk to the "other" database in a Python class,
so their methods are more or less like so:
def get_stuff(self, foo):
cursor = connections['otherConnection'].cursor()
stuff = cursor.execute('exec myStoredproc').fetchall()
return stuff

In and of themselves, the methods in this class work fine. Also note these
functions only retrieve data; there's no writing going on.

Now in the context of a view function I'm handling a posted form and this
interacts with the "primary" database connection (i.e. NOT the
'otherConnection' referenced above), and I'm using the
@transaction.commit_on_success decorator.

Inside that function I instantiate the class containing the methods like
the one above to retrieve data, and when I call that method it seems to
blow away my session because I'm logged out of the application and taken
back to my login page. If I change that method to NOT communicate with the
other database and instead just return dummy data I don't get kicked out of
the application.

Any ideas what's going on here? If the answer is that I can't do things
this way that's fine, but if that is the case what's the "right" way to
handle this situation?

Hopefully I explained that well but I'm happy to clarify as needed.

Thanks!

-- 
Matthew Woodward
m...@mattwoodward.com
http://blog.mattwoodward.com
identi.ca / Twitter: @mpwoodward

Please do not send me proprietary file formats such as Word, PowerPoint,
etc. as attachments.
http://www.gnu.org/philosophy/no-word-attachments.html

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



validation error in Django Admin

2012-12-12 Thread Mike Dewhirst
I'm getting an unexpected validation error in Admin which baffles me. 
Any hints appreciated ... here is the traceback


http://dpaste.com/844972/

At the bottom of the traceback where the error is raised, the local vars 
are ...


self
value   u'33'
key 'pk'

When I save the item, it auto-generates many-to-many connections to a 
few standard images in the database via a through table. However there 
is a rule which says "if ever these two images are connected to an item, 
only keep this one and drop that one."


I use a post-save signal to "drop that one" using ...

  Item_Pictogram.objects.filter(item=instance, pictogram=pic, 
via='auto').delete()


The local var value above being u'33' happens to be the exact pk of the 
Item_Pictogram record I want to delete - I checked in Postgres.


The pk should be an integer but I suppose that's nothing.

Thanks for any help

Mike

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



django-registration fork for 1.5?

2012-12-12 Thread mikegolf
Hi,
is there any django-registration fork that works on Django 1.5?

thanks,
mg

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/kia2ThjwpSwJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: select_for_update().get(...), what happens on DoesNotExist?

2012-12-12 Thread Chris Cogdon
The question is going to be very database specific. "select for update" 
works by putting in row-level locks in the database. If the row does not 
exist, then it won't get a lock on it, and I know of no databases that will 
maintain the query so that any newly created rows automatically get a lock 
on it.

However, not all is lost here.

When you create new rows, those rows are _invisible_ to any other 
transaction until you do a commit. So, as long as you arrange things so 
that there's a final commit after you've done all the manipulations you 
want, you won't have those rows stomped on by any other transaction.


>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/nZcB89jwNb4J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: ImageField in admin always required

2012-12-12 Thread Chris Cogdon


NOTE: I tried adding null=True, blank=True to forms.ImageField however I 
> get errors __init__() got an unexpected keyword argument so I guess I can't 
> use them options. If I comment out the def formfield_for_dbfield(self, 
> db_field, **kwargs): then the ImageField is not required as expected.
>

"ImageField is not required as expected" Isn't that what you want? :)

Anyway, the attribute for allowing a FormField to be "left blank" is 
required=False... blank=True and null=True are for ModelFields

If that knowledge doesn't give you any joy, try constructing a simple model 
that is not inherited from AbstractBaseUser... does that still end up 
having the picture be required?


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/zVyTxggDEdkJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Project path

2012-12-12 Thread Chris Cogdon
Not sure if you're willing to consider an alternate, here, but this is a 
issue Iv'e gone through a lot, and came up with my own solution. The major 
issue is that I not only want to change the top level paths, but also need 
to change database targets and a few other settings, between all the 
different dev/beta/production sites AND to make sure that the majority of 
my settings can be checked into our SCM. Here's what I came up with.

Rename settings to common_settings.py

Have a local_settings.py that is NOT checked into the SCM (perhaps provide 
a local_settings.py.example containing the minimum settings that need to be 
changed).

local_settings.py contains:

from projectname.common_settings import *

DATABASES = ...
SECRET_KEY = ...
STATIC_ROOT = ...

and anything else that _must_ change between installations.

Then change manage.py and wsgi.py so that it loads "common_settings" rather 
than "projectname.settings"

and done.

The BIG advantage here is that you're not checking anything into the SCM 
that must remain secret, or must change (or very likely to change) between 
installations, but all other settings are source controlled.

Although not necessary to rename settings.py, i do this to catch code that 
improperly imports settings, rather than django.conf.settings



>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/n_Se5QJGg6sJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Datastructure using django.

2012-12-12 Thread Chris Cogdon
I've no answers for you, but you might want to have a look at other 
applications that use those products and see how they work.

I know that "zinnia", a Django-based blog framework, uses "mptt", so you 
might want to have a look there. I've used zinnia, and I've never had to do 
any "reload" so to speak when I add in categories into the tree.

On Monday, December 10, 2012 10:15:41 AM UTC-8, Subhodip Biswas wrote:
>
> Hi all, 
>
> I am trying to implement a tree structure using django. There are two 
> packages I am looking at mptt and treebeard. Treebeard seems easy. 
>
> I have however two questions:
>
> 1. With mptt I found that if you have moved one child to another parent 
> both db and instance is updated but you need to reload or reregister every 
> time you call such action.Is there a better way of doing this?
>
> 2. If my child have more than one parent, the structure does not remain a 
> tree anymore. It becomes a graph i guess, How do you handle such situation. 
>
> 3. How do I add treebeard or mptt into django admin. If I have one field 
> name and I register it in admin.py. I get error regarding depth,path, 
> numchild and name should be present. How do I handle this situation.
>
> Apologies for asking so many questions.
>
>
> -
> Regards
> Subhodip Biswas
>
>
> GPG key : FAEA34AB
> Server : pgp.mit.edu
> http://subhodipbiswas.wordpress.com
> http:/www.fedoraproject.org/wiki/SubhodipBiswas
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/rK-CQW5LrTEJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Model method versus overriding save()

2012-12-12 Thread Chris Cogdon
On Sunday, December 9, 2012 9:08:18 PM UTC-8, Mike Dewhirst wrote:
>
> On 10/12/2012 10:59am, Victor Hooi wrote: 
> > Also - in terms of using them with QuerySets - there aren't any 
> > workarounds to use model methods with QuerySets are there? It seems like 
> > that would be a definite argument in favour of using the second method, 
> > right? 
>
> I'm not sure what you mean exactly here but you can use them in model 
> methods. Use the class manager. 
>
> ClassName.objects.filter(thing=self.this, whatever=self.that, etc=etc) 
>
 
What I believe Victor is asking for is that the model method is on the left 
hand side of that equals.

On my understanding of the framework, my best answer is "no", because the 
query system is unable to construct a SQL query that will encompass your 
model method, since that's in python.

This is a very classic "pre-calculate vs re-calculate" argument that a lot 
of applications go through. Django handles a bunch of these using "lazy" 
queries, that don't bother calculating things until it really needs them, 
but even so, needs some hints to get best performance (see things like 
select_related and prefetch_related). 

Here's some options for you:

1. Stick with a model method. Do most of your querying through django's 
ORM, but then do a post-filter in python using the results of the model 
method. This saves database space, and if the pre-filter result set is 
small, this will be pretty fast still. Howerver, if this is called a _lot_ 
you'll be hurt on performance.

2. Whenever you "save" a model, re-calculate the field and save it. This is 
very flexible, while chewing up a bit of space. This opens up the 
possibility of a desync between the source data and the calculated, but 
should be rare if you're never saving the model data outside of the save 
method (and your database handles transactions properly). Very fast if 
you're doing a lot of searching vs updating. But if you're updating FAR 
more often than running your search, you're wasting performance on the 
rarely used pre-calculation

3. If the calculation can be done at the database level, use the "extra" 
ORM method to do a search. This is better than 1, but will take a fair bit 
more database knowledge to get going, AND might tie you to a specific 
database implementation.

3a. Use a database "view" that presents the pre-calculation, making the ORM 
query a lot simpler. This means ensuring that you are allowed to update a 
view (postgresql 9.3 planned) or can somehow do a query on the view (which 
is a different table according to django). More django work here, and again 
might tie you to a particular database implementation


So, my guess is... as long as you're doing a reasonable number of "queries" 
versus "updates" to the table, to make the pre-calculation worthwhile, just 
go ahead and set that pre-calculation in the model's save() method,

 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/NkeuU2YvoaQJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



GeoDjango annotate distance

2012-12-12 Thread mikegolf
Hi,
I've recently started learning GeoDjango and I'm a bit confused. 
There's a GeoQuerySet method "distance" which annotates each object with 
the distance to the given point, like that:

pnt = 'POINT(coords... coords ...)'
MyModel.objects.all().distance(pnt)

but what field of the object does it take to calculate the distance?
I'd like to do "classic" annotation, like that:

MyModel.objects.all().annotate(distance_to_pnt = 
Distance('location_attribute_of_MyModel', pnt))

any tips?
thanks
mg

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/HsymmdttS_sJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



django-registration customization

2012-12-12 Thread sri
Hi,

I am using django-registration 
(http://pypi.python.org/pypi/django-registration) app for my project and i 
have added an extra page which shows both login and registration forms 
together.

But on this new page, the validation for registration form does not work. 
It works if i display the page with only registration form. But if the page 
has both registration and login forms the validation does not work. 

i have added below to the urls.py

url(r'^login_register/$',   login_register,  name='login_register'),

And in views.py, i have added the following.

def login_register(request, form_class=RegistrationForm, 
profile_callback=None):

if request.method == 'POST':

if 'login_button' in request.POST:
new_user = authenticate(username=request.POST['username'],
password=request.POST['password'])

if new_user is not None:
if new_user.is_active:
login(request, new_user)
else:
state = "Your username and/or password were incorrect."
form = RegistrationForm()
return 
render_to_response('registration/register_login.html',
 {'registerform': form,
 'login_state': state},
  context_instance=RequestContext(request))

return HttpResponseRedirect(request.GET.get("next"))

elif 'register_button' in request.POST:
form = RegistrationForm(data=request.POST, files=request.FILES)

if form.is_valid():
new_user = form.save(profile_callback=profile_callback)
new_user = authenticate(username=request.POST['username'],
password=request.POST['password1'])
login(request, new_user)
return HttpResponseRedirect(request.GET.get("next"))
else:
return 
render_to_response('registration/register_login.html',
 {'registerform': form},
  context_instance=RequestContext(request))
else:
form = RegistrationForm()
return render_to_response('registration/register_login.html',
{'registerform': form},
context_instance=RequestContext(request))


And my template for register_login looks like below.

{% block main_content %}
  

   Existing Customer (Login) 
  {% if login_state %}

   *{{ login_state }}

  {% endif %}

  {% csrf_token %}

  Username*
  

  


  Password*
  

  


  
Sign 
in
  

  



   New Customer (Register for an Account) 
  {% if register_state %}

   *{{ register_state }}

  {% endif %}
  {% csrf_token %}

  Username*
  

  


  Email 
Address*
  

  


  Password*
  

  


  Password 
(again)*
  

  


  Phone. No 
(Optional)
  

  


  
Register
  

  


  
{% endblock %}

{% block sidebar %}{% endblock %}


Now, when i click on the register button on the page, the form validation 
does not work.
Let's say if i enter the username that already exists on the database, it 
is not reporting any errors. It is just displaying the form without any 
error messages.
Can anyone help with what i am missing?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/e5M2K9Yu1mIJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



select_for_update().get(...), what happens on DoesNotExist?

2012-12-12 Thread Carsten Fuchs

Dear Django group,

we try to understand and solve a concurrency problem, and would kindly 
like to ask for your help.


We use Django 1.4, and a model like this:

class MonatsSalden(models.Model):
id= models.AutoField(primary_key=True)
key   = models.ForeignKey(Mitarbeiter)
jahr  = models.SmallIntegerField()
monat = models.SmallIntegerField()
saldo = models.DecimalField(max_digits=10, decimal_places=2)

class Meta:
unique_together = ('key', 'jahr', 'monat')


This is our code (for clarity, reduced to the essentials):

try:
CE = MonatsSalden.objects.get(jahr=Jahr, monat=Monat)

if meets_requirements(CE):
return CE
except MonatsSalden.DoesNotExist:
CE = MonatsSalden(key=self, jahr=Jahr, monat=Monat)

### Long and expensive computation to set the other fields of CE...
### (after it, meets_requirements(CE) returns True)
### ...

CE.save()
return CE


The code essentially implements the query of a cache ("CE" == "cache 
entry"), and returns the cached entry if it is not yet expired 
("meets_requirements"), or else completes and updates the cache.


The problem is that the long and expensive computation that is indicated 
with the ### comment lines can run long enough so that another view 
request can enter the same code in the meanwhile.


As a result, CE.save() can be called twice. (If CE was created anew, 
following the DoesNotExist exception, only the unique_together 
constraint prevents that duplicate rows are inserted, and raises a nice 
database error exception instead.)



To fix this, our plan was to first enable the TransactionMiddleware, 
because as we understand it, using select_for_update() is not very 
useful with the default open transaction behaviour, because the 
auto-commit that follows the end of the query immediately expires the 
locks. (Is this right??)


With the TransactionMiddleware, we would next add select_for_update():

CE = MonatsSalden.objects.select_for_update().get(jahr=Jahr, 
monat=Monat)


The intention is that if the code is re-entered in another process, it 
has to wait until the first process has finished dealing with its CE.


It seems like things work well when the DoesNotExist exception *not* 
occurs, because CE is locked, eventually returned, the view request is 
elsewhere soon completed, the transaction committed, and things are ok.


But... how does it behave when DoesNotExist occurs?

Will the previously established lock cover the newly created CE object 
even though it did not exist at the time of the select_for_update(), or not?


Many thanks in advance, any help is very much appreciated!
:-)

Best regards,
Carsten



--
   Cafu - the open-source Game and Graphics Engine
for multiplayer, cross-platform, real-time 3D Action
  Learn more at http://www.cafu.de

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Templates not found

2012-12-12 Thread donarb
When you used the shell, you imported 'tracks.views' but in your urls.py, 
you have musicsite.views.home. The two don't match.

On Wednesday, December 12, 2012 6:11:54 AM UTC-8, Lloyd Dube wrote:
>
> urls.py:
>
> urlpatterns = patterns('',
> # Examples:
> url(r'^$', 'musicsite.views.home', name='home'),
> # url(r'^musicsite/', include('musicsite.foo.urls')),
>
> # Uncomment the admin/doc line below to enable admin documentation:
> # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
>
> # Uncomment the next line to enable the admin:
> url(r'^admin/', include(admin.site.urls)),
> )
>
> I even considered indentation problems, but I have checked that and it is 
> fine.
>
>
> On Tue, Dec 11, 2012 at 5:53 PM, Sergiy Khohlov 
> 
> > wrote:
>
>> please paste your urls.py
>>
>>
>> 2012/12/11 Sithembewena Lloyd Dube >:
>> > Hi Segiy,
>> >
>> > Thanks for the response. "myproject" is only an alias I used while 
>> posting
>> > the code on here, in the project code it is actually "tracks".
>> >
>> >
>> > On Tue, Dec 11, 2012 at 1:31 PM, Sergiy Khohlov 
>> > > 
>> wrote:
>> >>
>> >> take a look  you have error related to myproject.view.home   but
>> >> importing track.view.home 
>> >>
>> >> 2012/12/11 Sithembewena Lloyd Dube >:
>> >> >
>> >> >
>> >> > On Tue, Dec 11, 2012 at 4:36 AM, Sithembewena Lloyd Dube
>> >> > >
>> >> > wrote:
>> >> >>
>> >> >> Hi all,
>> >> >>
>> >> >> I have a Django project running Django 1.4.1. (just upgraded). My
>> >> >> TEMPLATE_DIRS entry is:
>> >> >>
>> >> >> '/home/mymachine/Code/myproject/templates',
>> >> >>
>> >> >>
>> >> >> and that is exactly where the index template of the new project is 
>> even
>> >> >> when i use Firefox to locate it.
>> >> >>
>> >> >> In my home view, I call render_to_response as follows:
>> >> >>
>> >> >> 'def home(request):
>> >> >> return render_to_response('myapp/index.html', {})',
>> >> >>
>> >> >>
>> >> >> This raises an exception of type ViewDoesNotExist. If I run python
>> >> >> manage.py shell, import the view and call it with None as an 
>> argument
>> >> >> for
>> >> >> request, it shows that a response object is returned. Yet, the 
>> browser
>> >> >> will
>> >> >> not render it.
>> >> >>
>> >> >> >>> from tracks.views import home as h
>> >> >> >>> h(None)
>> >> >> 
>> >> >> >>> dir(h(None))
>> >> >> ['__class__', '__contains__', '__delattr__', '__delitem__', 
>> '__dict__',
>> >> >> '__doc__', '__format__', '__getattribute__', '__getitem__',
>> >> >> '__getstate__',
>> >> >> '__hash__', '__init__', '__iter__', '__module__', '__new__',
>> >> >> '__reduce__',
>> >> >> '__reduce_ex__', '__repr__', '__setattr__', '__setitem__',
>> >> >> '__setstate__',
>> >> >> '__sizeof__', '__str__', '__subclasshook__', '__weakref__',
>> >> >> '_base_content_is_iter', '_charset', '_container', 
>> '_convert_to_ascii',
>> >> >> '_get_content', '_headers', '_set_content', 'close', 'content',
>> >> >> 'cookies',
>> >> >> 'delete_cookie', 'flush', 'get', 'has_header', 'items', 'next',
>> >> >> 'set_cookie', 'set_signed_cookie', 'status_code', 'tell', 'write']
>> >> >>
>> >> >> Am I missing something?
>> >> >>
>> >> >> Thanks.
>> >> >>
>> >> >> --
>> >> >> Regards,
>> >> >> Sithu Lloyd Dube
>> >> >
>> >> >
>> >> >
>> >> >
>> >> > --
>> >> > Regards,
>> >> > Sithu Lloyd Dube
>> >> >
>> >> > --
>> >> > You received this message because you are subscribed to the Google
>> >> > Groups
>> >> > "Django users" group.
>> >> > To post to this group, send email to 
>> >> > django...@googlegroups.com
>> .
>> >> > To unsubscribe from this group, send email to
>> >> > django-users...@googlegroups.com .
>> >> > For more options, visit this group at
>> >> > http://groups.google.com/group/django-users?hl=en.
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google 
>> Groups
>> >> "Django users" group.
>> >> To post to this group, send email to 
>> >> django...@googlegroups.com
>> .
>> >> To unsubscribe from this group, send email to
>> >> django-users...@googlegroups.com .
>> >> For more options, visit this group at
>> >> http://groups.google.com/group/django-users?hl=en.
>> >>
>> >
>> >
>> >
>> > --
>> > Regards,
>> > Sithu Lloyd Dube
>> >
>> > --
>> > You received this message because you are subscribed to the Google 
>> Groups
>> > "Django users" group.
>> > To post to this group, send email to 
>> > django...@googlegroups.com
>> .
>> > To unsubscribe from this group, send email to
>> > django-users...@googlegroups.com .
>> > For more options, visit this group at
>> > http://groups.google.com/group/django-users?hl=en.
>>
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To post to this group, send email to django...@googlegroups.com
>> .
>> To unsubscribe from this group, send email to 
>> django-users...@googlegroups.com .
>> For more options, visit this group at 
>> http://groups.google.com/group/django-users?hl=en.
>>
>>
>
>
> -- 
> Regards,

Re: Templates not found

2012-12-12 Thread Sergiy Khohlov
 could you please  repeat your not view error ?
 Look like urls,oy has no access to views.py

Many thanks,
Serge


+380 636150445
skype: skhohlov


2012/12/12 Sithembewena Lloyd Dube :
> urls.py:
>
> urlpatterns = patterns('',
> # Examples:
> url(r'^$', 'musicsite.views.home', name='home'),
> # url(r'^musicsite/', include('musicsite.foo.urls')),
>
> # Uncomment the admin/doc line below to enable admin documentation:
> # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
>
> # Uncomment the next line to enable the admin:
> url(r'^admin/', include(admin.site.urls)),
> )
>
> I even considered indentation problems, but I have checked that and it is
> fine.
>
>
>
> On Tue, Dec 11, 2012 at 5:53 PM, Sergiy Khohlov  wrote:
>>
>> please paste your urls.py
>>
>>
>> 2012/12/11 Sithembewena Lloyd Dube :
>> > Hi Segiy,
>> >
>> > Thanks for the response. "myproject" is only an alias I used while
>> > posting
>> > the code on here, in the project code it is actually "tracks".
>> >
>> >
>> > On Tue, Dec 11, 2012 at 1:31 PM, Sergiy Khohlov 
>> > wrote:
>> >>
>> >> take a look  you have error related to myproject.view.home   but
>> >> importing track.view.home 
>> >>
>> >> 2012/12/11 Sithembewena Lloyd Dube :
>> >> >
>> >> >
>> >> > On Tue, Dec 11, 2012 at 4:36 AM, Sithembewena Lloyd Dube
>> >> > 
>> >> > wrote:
>> >> >>
>> >> >> Hi all,
>> >> >>
>> >> >> I have a Django project running Django 1.4.1. (just upgraded). My
>> >> >> TEMPLATE_DIRS entry is:
>> >> >>
>> >> >> '/home/mymachine/Code/myproject/templates',
>> >> >>
>> >> >>
>> >> >> and that is exactly where the index template of the new project is
>> >> >> even
>> >> >> when i use Firefox to locate it.
>> >> >>
>> >> >> In my home view, I call render_to_response as follows:
>> >> >>
>> >> >> 'def home(request):
>> >> >> return render_to_response('myapp/index.html', {})',
>> >> >>
>> >> >>
>> >> >> This raises an exception of type ViewDoesNotExist. If I run python
>> >> >> manage.py shell, import the view and call it with None as an
>> >> >> argument
>> >> >> for
>> >> >> request, it shows that a response object is returned. Yet, the
>> >> >> browser
>> >> >> will
>> >> >> not render it.
>> >> >>
>> >> >> >>> from tracks.views import home as h
>> >> >> >>> h(None)
>> >> >> 
>> >> >> >>> dir(h(None))
>> >> >> ['__class__', '__contains__', '__delattr__', '__delitem__',
>> >> >> '__dict__',
>> >> >> '__doc__', '__format__', '__getattribute__', '__getitem__',
>> >> >> '__getstate__',
>> >> >> '__hash__', '__init__', '__iter__', '__module__', '__new__',
>> >> >> '__reduce__',
>> >> >> '__reduce_ex__', '__repr__', '__setattr__', '__setitem__',
>> >> >> '__setstate__',
>> >> >> '__sizeof__', '__str__', '__subclasshook__', '__weakref__',
>> >> >> '_base_content_is_iter', '_charset', '_container',
>> >> >> '_convert_to_ascii',
>> >> >> '_get_content', '_headers', '_set_content', 'close', 'content',
>> >> >> 'cookies',
>> >> >> 'delete_cookie', 'flush', 'get', 'has_header', 'items', 'next',
>> >> >> 'set_cookie', 'set_signed_cookie', 'status_code', 'tell', 'write']
>> >> >>
>> >> >> Am I missing something?
>> >> >>
>> >> >> Thanks.
>> >> >>
>> >> >> --
>> >> >> Regards,
>> >> >> Sithu Lloyd Dube
>> >> >
>> >> >
>> >> >
>> >> >
>> >> > --
>> >> > Regards,
>> >> > Sithu Lloyd Dube
>> >> >
>> >> > --
>> >> > You received this message because you are subscribed to the Google
>> >> > Groups
>> >> > "Django users" group.
>> >> > To post to this group, send email to django-users@googlegroups.com.
>> >> > To unsubscribe from this group, send email to
>> >> > django-users+unsubscr...@googlegroups.com.
>> >> > For more options, visit this group at
>> >> > http://groups.google.com/group/django-users?hl=en.
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> >> Groups
>> >> "Django users" group.
>> >> To post to this group, send email to django-users@googlegroups.com.
>> >> To unsubscribe from this group, send email to
>> >> django-users+unsubscr...@googlegroups.com.
>> >> For more options, visit this group at
>> >> http://groups.google.com/group/django-users?hl=en.
>> >>
>> >
>> >
>> >
>> > --
>> > Regards,
>> > Sithu Lloyd Dube
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Django users" group.
>> > To post to this group, send email to django-users@googlegroups.com.
>> > To unsubscribe from this group, send email to
>> > django-users+unsubscr...@googlegroups.com.
>> > For more options, visit this group at
>> > http://groups.google.com/group/django-users?hl=en.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to django-users@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>
>
>
> --
> Regar

Re: Foreignkey Form

2012-12-12 Thread Sergiy Khohlov
https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-display/#detailview

Many thanks,
Serge


+380 636150445
skype: skhohlov


2012/12/12 Jonas Geiregat :
> Thanks for the response Serge,
>
> So basically I was on the right path.
>
> But I don't see what the DetailView has to do with this ? Don't you mean the 
> CreateView ?
>
> Jonas.
>
>> 1) you  should create a form for building
>> 2) you should create a form for inhabitant and set key to the  correct
>> value  before saving
>>
>> IMHO   simple way is create a edit form for buiding which has buttons
>> add/remove inhabitant .  check  DetailView  in docs please
>>
>> thanks, Serge
>> skype: skhohlov
>> tel: +380636150445
>>
>>
>> 2012/12/12 Jonas Geiregat :
>>> Hello,
>>>
>>> Suppose I have a model with a foreign key.
>>>
>>> class Building(models.Model):
>>>pass
>>> class Inhabitant(models.Model):
>>>building = models.ForeignKey(Building)
>>>
>>>
>>> I would like to display a form where all fields from the Building model and 
>>> Inhabitant model are visible (but only once)
>>>
>>> Should I create 2 forms, one for Building and one for Inhabitant ? And upon 
>>> submission add the Building to the Inhabitant.
>>>
>>> Currently I've worked together a little working something using the 
>>> CreateView (Generic views)
>>>
>>>
>>> def post(..)
>>>self.inhabitant_form = InhabitantForm(data=request.POST)
>>>if form.is_valid() and self.inhabitant_form.is_valid():
>>>….
>>>
>>> def form_valid(self, form):
>>># form is an instance of BuildingForm
>>>building = form.instance
>>>building.save()
>>>
>>># I'm also overriding the post method where self.inhabitant_form is 
>>> being set
>>>inhabitant = self.inhabitant_form.instance
>>>self.inhabitant.building = building
>>>self.inhabitant.save()
>>>
>>>
>>> Am I on the right path or are there are better ways of handling such 
>>> situations ?
>>>
>>> Regards,
>>>
>>> Jonas.
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups 
>>> "Django users" group.
>>> To post to this group, send email to django-users@googlegroups.com.
>>> To unsubscribe from this group, send email to 
>>> django-users+unsubscr...@googlegroups.com.
>>> For more options, visit this group at 
>>> http://groups.google.com/group/django-users?hl=en.
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To post to this group, send email to django-users@googlegroups.com.
>> To unsubscribe from this group, send email to 
>> django-users+unsubscr...@googlegroups.com.
>> For more options, visit this group at 
>> http://groups.google.com/group/django-users?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Unique Id issue

2012-12-12 Thread Jani Tiainen

12.12.2012 8:55, ephan kirjoitti:

Hi all,

Am in a bit of a fix which I put myself in,I have the following models:

class  Town(...):
 name = models.CharField(unique=true)
 province = models.foreignKey(Province)


class Area():
 name = models.CharField(unique=true,max_length=120)
 town = models.CharField(Town)


The problem here is I made the assumption that an area name is
unique,unfortunatly after deploying ,I have just realized that some
towns have the same area names so I can't have example area x in town A
as  well as area x in town B.

We have already deployed and have 575 users so far,is there any safe way
of removing the unique key constraint on Area model without damaging the
records which are already there?



You can remove it safely. Though you have to run some SQL script 
manually (or to use some migration tool like South) to remove it from 
the database(s).


--
Jani Tiainen

- Well planned is half done and a half done has been sufficient before...

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Implementing a monitoring system with django.

2012-12-12 Thread Ken Hill
I've been considering a very similar Django to help simplify some of my 
daily server system admin tasks (e.g. monitor and trend CPU uase, memory 
usage, disk space, etc.). I'd like to try CouchDB to store the data with a 
Django UI to make the app usable to non-technical users.

On Monday, December 10, 2012 11:41:14 AM UTC-8, Marc Aymerich wrote:
>
> Hi, 
> I'm considering to implement a simple monitorization system that 
> basically gathers some data from a set of nodes (<1000 nodes). The 
> gathered data should be stored in order to let users perform some 
> queries and maybe also generate some graphs. 
>
> I'm looking for advice in what components are best suited for each of 
> the following parts: 
>
> 1) Storage: maybe something nonsql like MongoDB? or perhaps RRD? 
> 2) Data gathering: celery periodic tasks? 
> 3) Graphs: rrd? or some java script framework for graphing? 
>
> thanks for your comments!! 
> -- 
> Marc 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/KNDp3QSOrx4J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Foreignkey Form

2012-12-12 Thread Jonas Geiregat
Thanks for the response Serge,

So basically I was on the right path.

But I don't see what the DetailView has to do with this ? Don't you mean the 
CreateView ?

Jonas.

> 1) you  should create a form for building
> 2) you should create a form for inhabitant and set key to the  correct
> value  before saving
> 
> IMHO   simple way is create a edit form for buiding which has buttons
> add/remove inhabitant .  check  DetailView  in docs please
> 
> thanks, Serge
> skype: skhohlov
> tel: +380636150445
> 
> 
> 2012/12/12 Jonas Geiregat :
>> Hello,
>> 
>> Suppose I have a model with a foreign key.
>> 
>> class Building(models.Model):
>>pass
>> class Inhabitant(models.Model):
>>building = models.ForeignKey(Building)
>> 
>> 
>> I would like to display a form where all fields from the Building model and 
>> Inhabitant model are visible (but only once)
>> 
>> Should I create 2 forms, one for Building and one for Inhabitant ? And upon 
>> submission add the Building to the Inhabitant.
>> 
>> Currently I've worked together a little working something using the 
>> CreateView (Generic views)
>> 
>> 
>> def post(..)
>>self.inhabitant_form = InhabitantForm(data=request.POST)
>>if form.is_valid() and self.inhabitant_form.is_valid():
>>….
>> 
>> def form_valid(self, form):
>># form is an instance of BuildingForm
>>building = form.instance
>>building.save()
>> 
>># I'm also overriding the post method where self.inhabitant_form is being 
>> set
>>inhabitant = self.inhabitant_form.instance
>>self.inhabitant.building = building
>>self.inhabitant.save()
>> 
>> 
>> Am I on the right path or are there are better ways of handling such 
>> situations ?
>> 
>> Regards,
>> 
>> Jonas.
>> 
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To post to this group, send email to django-users@googlegroups.com.
>> To unsubscribe from this group, send email to 
>> django-users+unsubscr...@googlegroups.com.
>> For more options, visit this group at 
>> http://groups.google.com/group/django-users?hl=en.
>> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Implementing a monitoring system with django.

2012-12-12 Thread Tom Evans
On Mon, Dec 10, 2012 at 7:41 PM, Marc Aymerich  wrote:
> Hi,
> I'm considering to implement a simple monitorization system that
> basically gathers some data from a set of nodes (<1000 nodes). The
> gathered data should be stored in order to let users perform some
> queries and maybe also generate some graphs.
>
> I'm looking for advice in what components are best suited for each of
> the following parts:
>
> 1) Storage: maybe something nonsql like MongoDB? or perhaps RRD?
> 2) Data gathering: celery periodic tasks?
> 3) Graphs: rrd? or some java script framework for graphing?
>
> thanks for your comments!!

Any reason not to use something like Nagios or Cacti? We use Nagios at
$JOB, it's great.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Foreignkey Form

2012-12-12 Thread Sergiy Khohlov
1) you  should create a form for building
2) you should create a form for inhabitant and set key to the  correct
value  before saving

 IMHO   simple way is create a edit form for buiding which has buttons
add/remove inhabitant .  check  DetailView  in docs please

 thanks, Serge
skype: skhohlov
tel: +380636150445


2012/12/12 Jonas Geiregat :
> Hello,
>
> Suppose I have a model with a foreign key.
>
> class Building(models.Model):
> pass
> class Inhabitant(models.Model):
> building = models.ForeignKey(Building)
>
>
> I would like to display a form where all fields from the Building model and 
> Inhabitant model are visible (but only once)
>
> Should I create 2 forms, one for Building and one for Inhabitant ? And upon 
> submission add the Building to the Inhabitant.
>
> Currently I've worked together a little working something using the 
> CreateView (Generic views)
>
>
> def post(..)
> self.inhabitant_form = InhabitantForm(data=request.POST)
> if form.is_valid() and self.inhabitant_form.is_valid():
> ….
>
> def form_valid(self, form):
> # form is an instance of BuildingForm
> building = form.instance
> building.save()
>
> # I'm also overriding the post method where self.inhabitant_form is being 
> set
> inhabitant = self.inhabitant_form.instance
> self.inhabitant.building = building
> self.inhabitant.save()
>
>
> Am I on the right path or are there are better ways of handling such 
> situations ?
>
> Regards,
>
> Jonas.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: ImageField in admin always required

2012-12-12 Thread Detectedstealth
Update:

Looks like it was an issue with the admin.py file custom code, I was 
following a code sample to display images inline which is causing the field 
to become required. Any suggestions on how to fix this? (See code below) 

class AdminImageFieldWithThumbWidget(forms.widgets.FileInput):

  def __init__(self, thumb_width=50, thumb_height=50):

self.width = thumb_width

self.height = thumb_height

super(AdminImageFieldWithThumbWidget, self).__init__({})


  def render(self, name, value, attrs=None):

thumb_html = ''

if value and hasattr(value, 'url'):

  print(value)

  thumb_html = '' % 
(value.url, self.width, self.height)

return mark_safe("%s%s" % (thumb_html, 
super(AdminImageFieldWithThumbWidget, self).render(name, value, attrs)))


class CustomUserAdmin(UserAdmin):

  def formfield_for_dbfield(self, db_field, **kwargs):

if db_field.name == 'picture':

  returnforms.ImageField(widget=AdminImageFieldWithThumbWidget(thumb_width 
= 
self.thumb_width, thumb_height = self.thumb_height))

return super(CustomUserAdmin,self).formfield_for_dbfield(db_field, 
**kwargs)

NOTE: I tried adding null=True, blank=True to forms.ImageField however I 
get errors __init__() got an unexpected keyword argument so I guess I can't 
use them options. If I comment out the def formfield_for_dbfield(self, 
db_field, **kwargs): then the ImageField is not required as expected.

On Thursday, December 6, 2012 2:47:57 PM UTC-8, Detectedstealth wrote:
>
> Hi,
>
> I have a picture for my custom user declared as follows:
>
> class CustomUser(AbstractBaseUser):
> picture = models.ImageField(upload_to='profile_pictures', null=True, 
> blank=True)
>
> In admin I use the image
>
> class CustomUserAdmin(UserAdmin):
> fieldsets(
> ('Profile details', {
> 'fields': (
> 'picture',
> 'status',
> )
> }),
> )
>
> Now for some reason even though I have null=True, blank=True in admin the 
> picture turns into a required field. Has anyone else experienced this or is 
> this expected behaviour ? 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/PlWOa--L5TAJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Foreignkey Form

2012-12-12 Thread Jonas Geiregat
Hello,

Suppose I have a model with a foreign key.

class Building(models.Model):
pass
class Inhabitant(models.Model):
building = models.ForeignKey(Building)


I would like to display a form where all fields from the Building model and 
Inhabitant model are visible (but only once)

Should I create 2 forms, one for Building and one for Inhabitant ? And upon 
submission add the Building to the Inhabitant.

Currently I've worked together a little working something using the CreateView 
(Generic views)


def post(..)
self.inhabitant_form = InhabitantForm(data=request.POST)
if form.is_valid() and self.inhabitant_form.is_valid():
….

def form_valid(self, form):
# form is an instance of BuildingForm
building = form.instance
building.save()

# I'm also overriding the post method where self.inhabitant_form is being 
set
inhabitant = self.inhabitant_form.instance
self.inhabitant.building = building
self.inhabitant.save()


Am I on the right path or are there are better ways of handling such situations 
?

Regards,

Jonas.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.