Re: Auto increase number number

2012-05-16 Thread Scott Gould
What is the purpose of the "no" field? If it's for display, it may be that you don't need to store it in the database at all. Something like: no, obj = enumerate(UserProfile.objects.all()) Even if it does need to be stored in the database, it's a calculated value that must be based upon the entir

Re: a very simple beginners question

2012-05-02 Thread Scott Gould
Are you looking for custom management commands? https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ On May 2, 8:59 am, alon wrote: > running > > $python manager.py shell > > opens a python shell > is there any way (a parameter) to make the manager run a python file > with my

Re: Django log lines spamming syslog

2012-03-23 Thread Scott Gould
Nail, meet head. Thanks! I was thinking along similar lines but stupidly checked everywhere (my code, apache confs, etc.) except the django source on the server itself. On Mar 23, 11:36 am, Reinout van Rees wrote: > On 23-03-12 14:49, Scott Gould wrote: > > > > > Our syslog h

Django log lines spamming syslog

2012-03-23 Thread Scott Gould
Hi folks, Our syslog has been filling up for some time with this stuff and we only just noticed due to logrotate breaking on us. For what appears to be every django request across all virtual hosts, we are getting a pair of lines, like so (blank lines inserted by me): Jan 27 14:48:52 cloweb01 apa

Re: Time duration in django models

2011-08-09 Thread Scott Gould
Nothing native. I'd store it in seconds, as an int, and let the model convert to and from a timedelta. Looks like there's a snippet all ready to go: http://djangosnippets.org/snippets/1060/ On Aug 9, 6:32 am, Mohamed Ghoneim wrote: > Hey guys, > > I am just wondering if there is a time duration

Re: Accessing "request" from within an authentication backend

2011-07-26 Thread Scott Gould
I'd raise a custom exception (e.g. "PasswordExpiredError") and handle the message creation in the view. On Jul 26, 3:59 pm, Steven Smith wrote: > Is there a way to access the HttpResponse, or issue a redirect from > within a custom authentication backend? I have Django hooked up to our > Active D

Re: Selecting distinct months from a QuerySet

2011-05-19 Thread Scott Gould
I'd either add a manager with a method that did the query you describe in SQL, or (if you're wanting the whole queryset anyway) just calculate it via: months = list(set(x.datetime_field.month for x in queryset)) months.sort() On May 19, 1:03 am, Ian Turner wrote: > Hi, > > I would like t

Re: forms.ChoiceField get displayed value

2011-01-20 Thread Scott Gould
Try something like this: field = form.fields['property'] data = form.cleaned_data['property'] if isinstance(data, (list, tuple)): # for multi-selects friendly_name = [x[1] for x in field.field.choices if x[0] in data] else:

Re: A few beginner questions

2011-01-05 Thread Scott Gould
On Jan 4, 9:38 pm, Shawn Milochik wrote: > Hi Ondřej. > > ... > #2: The validation should all be done during form validation[2], prior to > save. Using a Form or ModelForm. That way, the user can get friendly, useful > errors. Further to this, look into model validation if the logic is integral

Re: auth_user first_name and last_name editing

2010-11-24 Thread Scott Gould
A simple custom ModelForm should do the trick: from django.contrib.auth.models import User class UserChangeForm(forms.ModelForm): class Meta: fields = ('first_name', 'last_name',) model = User On Nov 24, 1:33 pm, BozoJoe wrote: > HI, > > I'm using django

Re: table namespace hygiene: should I try to rename auth_* tables, or should I use a separate database?

2010-11-01 Thread Scott Gould
> If not, I could create a new database, and devote it to the django > stuff. Is that a good solution? Are there significant disadvantages to > using a separate mysql database just for the django stuff? Is there > maintenance overhead for the dba? (I don't know.) Are there any > disadvantages, say,

Re: How to aggregate values by month

2010-10-28 Thread Scott Gould
Personally I hate writing raw SQL so I would probably try something like this (untested): sales = Sale.objects.filter(date_created__range=(init_date,ends_date)) .values(date_ created__month) .aggregate(total_sales=Sum('total_value')) sales_by_month = [(x.year, x.month, [y for y in sales i

Re: Access to field name in upload_to?

2010-10-26 Thread Scott Gould
> One idea would be to put 'fieldname' as the first parameter to the > function, then use functools.partial [1] to create partial functions > for each file field with the value set appropriately: > >         thumbnail_image = FileField(upload_to=partial(get_upload_path, > 'thumbnail_image')) Outst

Access to field name in upload_to?

2010-10-26 Thread Scott Gould
Hi folks, I've got all my file uploads (that go to S3 as it happens, but I don't think that's overly important) taking their path from one upload_to delegate: def get_upload_path(instance, filename=None): """ Defaults to appname/modelname/uuid. """ return "%s/%s/%s

Re: Query field across multiple django models

2010-10-26 Thread Scott Gould
> Second, I'm not sure I understood the last part about getting the > field within each model. Sorry, I misread, thinking you were talking about having different parameters of each model being responsible for what counted as "latest". Bruno's solution looks good to me. -- You received this messa

Re: Query field across multiple django models

2010-10-26 Thread Scott Gould
Maybe -- in fact, almost certainly not -- the best way, but this is how I do that kind of thing: a_queryset = ModelA.objects.all() another_queryset = ModelB.objects.filter.(by_something=True) yet_another_queryset = ModelC.objects.exclude(by_something_else=False) fr

Re: call more than one view action from an uri path?

2010-10-21 Thread Scott Gould
What's your use case? Are "nest, pest and rest" always "nest, pest and rest" -- or could they be "rest, pest and nest", or "nest, best, and rest"? If they're set in stone; that is, it's always nest, followed by pest, followed by rest, then it's easy enough to just parameterize the url: '/nest

Re: Best way to change pk field type

2010-10-11 Thread Scott Gould
With a huge whopping disclaimer that I've never done this before: Wouldn't you simply be abe to add an explicit "id = BigIntegerField(primary_key=True)" to the model and change the column type accordingly in mysql? On Oct 7, 4:16 pm, indymike wrote: > Here's my issue - I'm going to run out of in

Re: Django regroup not working as expected

2010-10-07 Thread Scott Gould
Your .list is in the wrong place: {% for date in sale_list%} {{ date.grouper }} {% for sale in date.list %} {{ sale.item }} - {{ sale.qty }} {% endfor %} {% endfor %} Regards Scott On Oct 5, 2:28 pm, Gath wrote: > Guys > > I have the follow

Re: convert list into a comma separated string

2010-10-04 Thread Scott Gould
Python has you covered: my_string = ",".join(my_list) HTH, Scott On Oct 4, 7:41 am, ashy wrote: > Hi All, > > I am writing a django function in which I have a following list: > li = ['a','b','c'] > I want to create a string from the list which should look like > str = 'a,b,c' > > I plan to pass

Re: Python unexpectedly quit

2010-09-24 Thread Scott Gould
> def logout(request): >     logout(request) Infinite loop, no? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+uns

Re: Possible to have dynamic drop down list in ModelForm?

2010-09-20 Thread Scott Gould
Two ways, both requiring javascript: 1. Populate the city dropdown via ajax on country change. 2. Have your template write javascript to do country->city lookups and alter the contents of the city dropdown on country change. On Sep 19, 2:42 am, Andy wrote: > I have a model FieldReport that has,

Re: Using IDs for upload_to path

2010-09-14 Thread Scott Gould
Your ID: if it's a new object, you can't. Another field in the model: if it's calculated instead of database- provided, sure. I use UUIDs for this: def get_uuid(): import uuid return str(uuid.uuid4()) class UploadedFileModel(models.Model): physical_file = FileField(verbos

Re: Alternative to passing request object to every object/function?

2010-09-02 Thread Scott Gould
Have a read through this article, Dan: http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/ On Sep 2, 11:19 am, Dan Klaffenbach wrote: > On 2 Sep., 16:48, Daniel Roseman wrote:> It's not > clear exactly what you want. Using RequestContext and the > > request context processor will ensure th

Re: Prepopulate some fields in a admin add form

2010-08-23 Thread Scott Gould
First, just to make absolutely clear you understand, the "user's name" isn't the foreign key itself -- that would be the user's ID. The user's name is what would be displayed to help you *select* the correct ID, under the hood. To the question at hand, it's quite simple to add a method to your Mod

Re: What is the best way to manually create an HTML form from a ModelForm

2010-08-23 Thread Scott Gould
You can refer to individual fields by name, for example: > >     {% form.first_name %} > Looping through them works fine for simple forms but as you say, if you want something more elaborate in your template, you sometimes need to go field-by-field. What I do is write an inclusion tag with all

Re: multiple forms for same model

2010-08-16 Thread Scott Gould
You'll want to call is_valid() only if the request.method is "POST", plus your code leaves the possibility of an error if none of those permissions is found (your elif should probably simply be an else?). On Aug 16, 6:49 am, Mess wrote: > Hi, > > I have a model where different users should have d

Re: ManyToManyField limiting choices

2010-08-10 Thread Scott Gould
Define a custom ModelForm for your model that sets the appropriate queryset parameter (either altering the field in __init__, or just defining the field in the class as a ModelMultipleChoiceField). Then register that ModelForm for use in the admin (via a ModelAdmin class). On Aug 9, 7:51 pm, Marti

Re: How to get the model name of an object in template?

2010-08-04 Thread Scott Gould
Not that I'm aware of, in which case I'd say a tag (or even a filter) *is* the "built-in way". No great hardship: @register.filter def class_name(value): return value.__class__.__name__ On Aug 4, 11:13 am, "David.D" wrote: > There's no django's

Re: How to get the model name of an object in template?

2010-08-04 Thread Scott Gould
How about writing a simple template tag that takes an object and returns object.__class__.__name__? On Aug 4, 10:20 am, "David.D" wrote: >  I did it by adding this to my models: >         def get_model_name(self): >                 return self.__class__.__name__ > > And it works > > But I don

Re: values with distinct not working as expected

2010-07-30 Thread Scott Gould
You don't need order_by to get distinct values per se, but in this case you need to put it in to override whatever default ordering you have on your model (since any order_by fields will be included in the query and thwart your distinct()). On Jul 30, 3:52 am, tuxcanfly wrote: > Thanks, that work

Re: single point of entry to a webpage

2010-07-26 Thread Scott Gould
I often cringe it when people trot out the "read the docs" line, but that's honestly what it comes down to in this case. An app isn't some sacred walled city. Just write a template tag, import whatever you need from whichever apps they belong to, and stick it on the page. But before you do that, r

Re: filter users by full name

2010-07-22 Thread Scott Gould
It won't work because there's no database column that corresponds to the full name. A simple but limited alternative would be to split the string on " " and use the result as first_name and last_name, but you would probably want to take into account first_names and/or last_names with legitimate sp

Re: distinct() ?

2010-07-21 Thread Scott Gould
Does your model refer to any other fields for its default ordering? http://docs.djangoproject.com/en/1.2/ref/models/querysets/#distinct On Jul 21, 1:06 pm, rmschne wrote: > I'm trying to use the distinct() and it's giving me unexpected > results.  Surely it's something simple, but I can't spot

Re: best practice for widget that edits multiple model fields

2010-07-21 Thread Scott Gould
    all_day = BooleanField() > > # subclass DateRange so we can reuse it in various models > class EventDateRange(DateRange): >     date = ForeignKey(Event) > > class Event(Model): >     #various other fields required for event > > Does this seem like it would make more sense? &

Re: best practice for widget that edits multiple model fields

2010-07-21 Thread Scott Gould
My immediate thought upon reading your post was that you should build an accompanying custom model field. Having said that, I haven't had cause to do one that involved (only extend -- simply -- the stock fields) so can't provide any specifics. http://docs.djangoproject.com/en/1.2/howto/custom-mode

Re: forms

2010-07-20 Thread Scott Gould
First things first: 1. Do you have a Model for your data? 2. Is your form a ModelForm attached to this Model? On Jul 20, 2:14 pm, commonzenpython wrote: > VolunteerForm is the name of my form class in forms.py, i put the code > you gave me : > > if request.POST: >         form = VolunteerForm(re

Re: ordering a model on multiple fields?

2010-07-15 Thread Scott Gould
The Django *admin* only uses one field, ever. Bit of an irritating limitation, I grant you, but with the use of date hierarchies and list filters it's not too bad. On Jul 14, 7:40 pm, hjebbers wrote: > is there a way to have a model class sorted on multiple fields? > in the meta class of my mode

Re: Silly Question? Mulitple Checkbox Processing

2010-07-13 Thread Scott Gould
Try it and see :) If the checkboxes share the same name (as in the name attribute of the input tag), then the checked ones' values form a comma-delimited list. On Jul 13, 11:19 am, zippzom wrote: > Maybe this is a silly question, but if I have a form with multiple > checkboxes that are outputted

Re: __init__ custom form validation

2010-07-13 Thread Scott Gould
Your init is expecting a value for field3_type, so you need to provide it when you create the form in your view (prior to request.POST in the postback instance). On Jul 12, 2:44 pm, Nick wrote: > I am working on a validation that will require a field be filled in if > it is another field enters o

Re: Deployment Permissions

2010-06-28 Thread Scott Gould
As long as the last number is 4 or greater (at least read access for all) you should be fine. Regards Scott On Jun 27, 5:50 am, Rusty Shackleford wrote: > Hi, > > I'm about to deploy my first Django app, but I have a few questions > about permissions. > > The project container is at '/usr/local/

Re: outer join in orm

2010-06-21 Thread Scott Gould
There may well be a better way to do this, especially since it's been a good year since I was struggling with this myself. (Very similar case to yours, different subject matter of course.) The way I ended up doing it was to use a template tag and some list comprehensions to whittle things down. E.

Re: not able to recognize non-default python files in app + project directory

2010-06-11 Thread Scott Gould
Apologies for the "__initial__" stuff -- damn iPhone autocomplete! On Jun 11, 11:15 am, rahul jain wrote: > It was rw-r--r-- . I also modified it to 777 by" chmod -R 777". But > did not fix my problem. > > This is the error which I am getting > ImportError: cannot import name > > --RJ > > > > On

Re: annotate with query set for distinct values

2010-06-11 Thread Scott Gould
It's not really a matter of "working around" it. Your .xxx method/ property is an attribute of the object. What you evidently want from the database is *not* a list of those objects, but rather a summary representation of them. Trying to apply your .xxx is meaningless as you don't have a discrete o

Re: not able to recognize non-default python files in app + project directory

2010-06-11 Thread Scott Gould
Did you put Test1.py and Test2.py into a "models" directory where models.py would normally be, and add an __initial__.py file to it? On Jun 10, 5:18 pm, rahul jain wrote: > HI Dan, > > Thanks for your response but that will not solve my problem. > > I am not splitting models. I am splitting actio

Re: ordering integers with regroup

2010-06-09 Thread Scott Gould
If you're ordering on "District 1", "District 2", etc. then the number is part of a string and will be sorted alphabetically. I image your only recourse will be to use the numeric field directly, and deal with prepending "District " to it in some other fashion. On Jun 9, 10:38 am, Nick wrote: > H

Ensuring a single file in storage, per FileField

2010-06-04 Thread Scott Gould
Hi folks, I have various models with File and Image fields. I want to keep the filesystem storage clean, removing any orphaned file upon a new one being uploaded. To date I've been overriding save() in each model, which isn't terribly robust: class MyModel def save(self, **kwargs):

Re: How to dynamically set limit_choices_to for ManyToManyField using values from an instance?

2010-06-04 Thread Scott Gould
4, 8:50 am, Massimiliano della Rovere wrote: > Thanks Scott, > I have one doubt though: I need a bound form to properly set the queryset. > Using your method I'd use an unbound form, am I wrong? > > > > On Fri, Jun 4, 2010 at 14:33, Scott Gould wrote: > > Create a

Re: How to dynamically set limit_choices_to for ManyToManyField using values from an instance?

2010-06-04 Thread Scott Gould
Create a ModelForm for your model (A), set the queryset to what you want on the appropriate field (n), and then register that ModelForm for admin use in your admins.py. Regards Scott On Jun 4, 6:14 am, Massimiliano della Rovere wrote: > When displayed in the Admin interfaces, I'd like to filter

Re: Multitable inheritance - reverse relation

2010-05-27 Thread Scott Gould
Try: myB = B.objects.get(pk=1) myCs = C.objects.filter(a__b=myB) Regards Scott > I have model A in which there is field ForeignKey(B). I also have > model C which inherits from model A. None of these models is abstract. > Having B object, can I find all C objects pointing to it? When I use > som

Re: making this app re-usable

2010-05-27 Thread Scott Gould
You're after get_model in django.db.models: model_class = get_model(*model_string.split('.')) See James Bennet's post in this topic: http://www.b-list.org/weblog/2007/nov/03/working-models/ Regards Scott On May 27, 12:13 am, Jeffrey Taggarty wrote: > Hey Guys, > > I am trying to build th

Re: Post and Comment with the same Model.

2010-05-25 Thread Scott Gould
>From your description thus far I'd probably try to use django.contrib.comments and be done with it. Build the specific models you need for your app, but don't reinvent the wheel when there's a perfectly good generic commenting app out there already. On May 25, 12:01 pm, nameless wrote: > mmm so

Re: ordering inside regroup

2010-05-25 Thread Scott Gould
On May 25, 3:57 am, maciekjbl wrote: > Thank you, for your answer. > > I didn't have time to check this because of flood in my company. > > I thought that 'regroup' can't be order by any other sorting beside > it's own. > > So I have to order it right and then use 'regroup' or 'regroup' then > ord

Re: Post and Comment with the same Model.

2010-05-25 Thread Scott Gould
Presumably you want the same object to be able to be a comment on someone else's post as well as a post in it's own right, that others could comment on? Looks straightforward enough, though I can't see why you'd need the generic fields in the model. Regards Scott On May 25, 5:54 am, nameless wro

Re: inclusion tag variable resolving

2010-05-24 Thread Scott Gould
Can you not simply pass foo: {% make_button 'click me' foo %} ... resolve it, and then use either its .get_absolute_url() or something other url, depending on what foo is? The inclusion tag's python code is far more equipped to deal with logic decisions, special cases, etc. than the template is.

Re: Items from multiple tables on one form?

2010-05-24 Thread Scott Gould
> Recall I'd like to make a table with each row in the table being a > "form" for a paired Author-Book item (recall my book-author is 1-1). > If I make an Author formset and a Books formset and then render them > in the template it's not clear to me that the book part of the form > would in fact co

Re: Unicode Error when Saving Django Model

2010-05-24 Thread Scott Gould
Point taken, three times. On May 24, 9:40 am, Karen Tracey wrote: > On Mon, May 24, 2010 at 8:27 AM, Scott Gould wrote: > > > My database and all of its tables are UTF8 encoded with UTF8 collation > > > (DEFAULT CHARSET=utf8;) > > > The data I am inputting is unic

Re: Unicode Error when Saving Django Model

2010-05-24 Thread Scott Gould
> My database and all of its tables are UTF8 encoded with UTF8 collation > (DEFAULT CHARSET=utf8;) > The data I am inputting is unicode > (u'Save up to 25% on your online order of select HP LaserJet\x92s') > > > But when I try to save this data I get an error > Incorrect string value: '\\xC2\\x92s

Re: Items from multiple tables on one form?

2010-05-24 Thread Scott Gould
> # Models > class Author(models.Model): >     name = models.CharField(max_length=100) >     address = models.CharField(max_length=100) > > class Book(models.Model): >     author_id = models.OneToOneField(Author) >     title = models.CharField(max_length=100) > > I'd like to have a form that allowe

Re: Multiple apps in one view

2010-05-24 Thread Scott Gould
> Can you run multiple apps in one view, and what would be the best way > to do that. Firstly you can't "run an app" as such. Think of apps as folders on your desktop -- they organise and compartmentalise in Django. > Let's say I have two apps and I want to display some items from app1 > and some