Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
DICTATOR OF FUNK
Nov 6, 2007

aaaaaw yeeeeeah

Lamacq posted:

I guess I don't know what you mean by "blank". some_order.assigned_to.all() should at least return an empty list. Are you sure the order instance you're working with in the interactive interpreter is the same one you're updating via the form? (I'm assuming you're using "./manage.py shell" to play with your django models in the interactive interpreter)
My bad, it does return an empty QuerySet list (<bound method QuerySet.all of []>). I have tested this in both my app and the interactive shell and it gets an empty list both times.

It just seems that the ManyToManyField table isn't being populated when I submit orders, but the main Orders table is.

Adbot
ADBOT LOVES YOU

Lamacq
Jun 15, 2001

Breezeblock RIP

root beer posted:

My bad, it does return an empty QuerySet list (<bound method QuerySet.all of []>). I have tested this in both my app and the interactive shell and it gets an empty list both times.

It just seems that the ManyToManyField table isn't being populated when I submit orders, but the main Orders table is.

And but when you go back to the form to edit the order that you've just saved (and presumably assigned to users), are those users still selected in the "assigned to" multiple select? Care to put up a paste of your form code, template, and the view function?

DICTATOR OF FUNK
Nov 6, 2007

aaaaaw yeeeeeah

Lamacq posted:

And but when you go back to the form to edit the order that you've just saved (and presumably assigned to users), are those users still selected in the "assigned to" multiple select? Care to put up a paste of your form code, template, and the view function?
No, they aren't selected still.

Here's the relevant code:
Relevant views: http://pastebin.com/cESdt7bx

Template: http://pastebin.com/90bCg6P1

Form code (lol):
code:
class TicketCreate(ModelForm):
    class Meta:
        model = Order

Lamacq
Jun 15, 2001

Breezeblock RIP
VVVVVVVVVVV what he said

Lamacq fucked around with this message at 21:25 on Mar 31, 2011

Yay
Aug 4, 2007

Lamacq posted:

Note that you never instantiate Order objects directly; you use the Order.objects class attribute (which is a Model Manager) to create or retrieve Order instances.
Additional note: calling form.save() should do the work of Order.objects.create for you. (Aside: what's wrong with creating instances of Order()? I've never encountered anything that said that was bad, nor have I had any problems with it)

code:
if form.is_valid()
    order = form.save(commit=False)
    order.authors.add(request.user)
    order.save()
    return redirect(order)

DICTATOR OF FUNK
Nov 6, 2007

aaaaaw yeeeeeah

Yay posted:

Additional note: calling form.save() should do the work of Order.objects.create for you. (Aside: what's wrong with creating instances of Order()? I've never encountered anything that said that was bad, nor have I had any problems with it)

code:
if form.is_valid()
    order = form.save(commit=False)
    order.authors.add(request.user)
    order.save()
    return redirect(order)
Thanks for the help guys, I'll give this a try. It seems like things work better when I let Django deal with that kind of dirty work.

Surface
May 5, 2007
<3 boomstick

Yay posted:

(side: what's wrong with creating instances of Order()? I've never encountered anything that said that was bad, nor have I had any problems with it)

Nothing, but be sure to call .save() on the instance if you want it to actually exist, also you can't establish FK/M2M relations until you call .save() because until you do it won't have an id assigned to it.

ufarn
May 30, 2009
Any good 1.2 1.3 learning resources? All the guides I've found refer to 1.0 and 1.1, and it usually makes it impossible to follow them from a beginner level.

ufarn fucked around with this message at 00:33 on Apr 1, 2011

king_kilr
May 25, 2007

Yay posted:

Additional note: calling form.save() should do the work of Order.objects.create for you. (Aside: what's wrong with creating instances of Order()? I've never encountered anything that said that was bad, nor have I had any problems with it)

code:
if form.is_valid()
    order = form.save(commit=False)
    order.authors.add(request.user)
    order.save()
    return redirect(order)

You probably need `order.save_m2m()` as well.

Lamacq
Jun 15, 2001

Breezeblock RIP

king_kilr posted:

You probably need `order.save_m2m()` as well.

`form.save_m2m()` I think you mean. `save_m2m` is defined on the ModelForm, not the Order instance IIRC.

DICTATOR OF FUNK
Nov 6, 2007

aaaaaw yeeeeeah

Lamacq posted:

`form.save_m2m()` I think you mean. `save_m2m` is defined on the ModelForm, not the Order instance IIRC.

Yep, changing my model to

code:
if form.is_valid():
    order = form.save(commit=False)
    order.author = request.user
    order.save()
    form.save_m2m()
and now everything works perfectly. Thanks!

Edit: I lied. Now I added a ForeignKey relationship from the Orders model to an Orders_Description model, and although the form correctly generates a nice big <textarea> for me, but the form throws an error and says the description field is blank whether it is or not.

I had this happen to me once before and I fixed it by upgrading from 1.2.5 to 1.3. Should I just install nightly builds every time I see this error? :v:

Edit2: Every time I try to submit the form, it says "Select a valid choice. That choice is not one of the available choices." for the description ForeignKey. The form is generating it as a <textarea> as expected. What the gently caress?

DICTATOR OF FUNK fucked around with this message at 18:24 on Apr 1, 2011

king_kilr
May 25, 2007

Lamacq posted:

`form.save_m2m()` I think you mean. `save_m2m` is defined on the ModelForm, not the Order instance IIRC.

:commissar:, I think I'm going to go commit something to forms to drown my sorrows.

Lamacq
Jun 15, 2001

Breezeblock RIP

king_kilr posted:

:commissar:, I think I'm going to go commit something to forms to drown my sorrows.

No worries dude. You've committed about a billion times more worth to python & django than I ever will.

Infereit
Dec 29, 2000

If any Django developers here are looking for a part time / contracting remote engagement with the possibility of full time employment if things go well, please send me a PM with note about your experience, a resume and phone number.

Background: In January, I cofounded a company in the advertising optimization space and we have had a reasonable amount of success but are unable to build things as fast as we would like and have a large backlog of projects. We are based in San Francisco.

The other cofounder and I both have an engineering background. I've been working with Django for about 6 months, he has been working with it for 3.5 years and knows it inside and out.

If this is the wrong place for this, I'm happy to move it to another thread or forum.

hitze
Aug 28, 2007
Give me a dollar. No, the twenty. This is gonna blow your mind...

I've been hunting around trying to find the answer for this, and only found information regarding normal forms. Where do you put custom validation/clean for a ModelForm? I have a field that I need to strip out everything but numbers, and check if its below a certain number.

Yay
Aug 4, 2007

hitze posted:

I've been hunting around trying to find the answer for this, and only found information regarding normal forms. Where do you put custom validation/clean for a ModelForm? I have a field that I need to strip out everything but numbers, and check if its below a certain number.
ModelForms are (simplistically speaking) just Forms bound to a Model class, so the topic on Form validation is still relevant; see cleaning a specific field, and just below that, the notes on inter-dependent cleaning.

Edit: for your purposes, models.PositiveIntegerField, combined with a defined integerfield with max_value ought to work.

Yay fucked around with this message at 18:55 on Apr 8, 2011

Lamacq
Jun 15, 2001

Breezeblock RIP
I just released this, hope somebody finds it useful.

epswing
Nov 4, 2003

Soiled Meat

Lamacq posted:

I just released this, hope somebody finds it useful.

This looks great, I was wishing for something like this last month. How does it play with 1.3?


vvv Edit: Descent: Freespace :keke:

epswing fucked around with this message at 20:08 on Apr 8, 2011

Lamacq
Jun 15, 2001

Breezeblock RIP

epswing posted:

This looks great, I was wishing for something like this last month. How does it play with 1.3?

I tested it with 1.3, if you have any problems go ahead and file a bug report :)

BTW is your avatar from Ground Control? I really should finish that game one of these days...

hitze
Aug 28, 2007
Give me a dollar. No, the twenty. This is gonna blow your mind...

Yay posted:

ModelForms are (simplistically speaking) just Forms bound to a Model class, so the topic on Form validation is still relevant; see cleaning a specific field, and just below that, the notes on inter-dependent cleaning.

Edit: for your purposes, models.PositiveIntegerField, combined with a defined integerfield with max_value ought to work.
Managed to finally figure it out. I ended up changing the field to a CharField since it would want to validate the IntegerField (Must be whole number, etc.) before my custom clean. Ended up with this little bit of code.
code:
	def clean_desiredPrice(self):
		desired = self.cleaned_data['desiredPrice']
		current = self.data['currentPrice']
		cleaned = filter(lambda x: x.isdigit(), desired)
		
		if cleaned > current:
			raise forms.ValidationError("desired price needs to be lower than current price.")
		return cleaned

hitze fucked around with this message at 03:22 on Apr 10, 2011

epswing
Nov 4, 2003

Soiled Meat
I'm trying to put together what I need for django-registration to work...I'm designing activation_email.txt which contains a link users should click to activate their account.

It appears {% url registration_activate activation_key=activation_key %} returns /accounts/activate/somehashgoeshere/ while {{ site }}/accounts/activate/{{ activation_key }}/ produces example.com/accounts/activate/somehashgoeshere/

I'm not sure where example.com is defined and where I can override it. The settings.py file has a SITE_ID but I don't think that's what I'm looking for.

Google searches are getting me unrelated pages, I'm probably not searching for the right keyword. Any advice?

Lamacq
Jun 15, 2001

Breezeblock RIP

epswing posted:

I'm trying to put together what I need for django-registration to work...I'm designing activation_email.txt which contains a link users should click to activate their account.

It appears {% url registration_activate activation_key=activation_key %} returns /accounts/activate/somehashgoeshere/ while {{ site }}/accounts/activate/{{ activation_key }}/ produces example.com/accounts/activate/somehashgoeshere/

I'm not sure where example.com is defined and where I can override it. The settings.py file has a SITE_ID but I don't think that's what I'm looking for.

Google searches are getting me unrelated pages, I'm probably not searching for the right keyword. Any advice?

Look in your database for a table called ``django_site``. The SITE_ID in your settings file is referencing a row in this table; by default the first record has the domain "example.com".

mwarkentin
Oct 26, 2004
Yep, you can edit it in the admin.

Hed
Mar 31, 2004

Fun Shoe
Anyone have experience playing with Django and Google Apps? I'm in the design stages of an application for a domain that has business hosted Gmail. I'd love it if there's a way to authenticate users with Google Accounts but Google's single sign on documentation isn't exactly clear on this subject unless I'm explicitly using Google App Engine or something that appears to support Django 0.97.

duck monster
Dec 15, 2004

edit: Nevermind hosed up the url.py

duck monster fucked around with this message at 09:14 on Apr 29, 2011

Yay
Aug 4, 2007
Is there a better way of getting all instances in a ModelFormSet than something like the following:

code:
qs = Thing.objects.all()
formset = modelformset_factory(Thing)
thing_formset = formset(queryset=qs)
# do some stuff
if thing_formset.is_valid():
    changed_instances = thing_formset.save()
    # the line below is the one I'm wondering about.
    all_instances = set(list(qs) + list(changed_instances))
Note that the code above may not be valid, I was just thinking about a colleague's work problem, and this is the best I can think of.

duck monster
Dec 15, 2004

I'm having a hell of a time with South (yet again! I'm becoming very unclear as to the benefit of this loving thing vs just manually tracking database changes when its so god drat fragile and prone to exploding) convincing it to shut the gently caress up about django-cms. The thing seems to have some very broken migrations and whilst it seems it'll install fine with a syncdb, it just vomits blood anywhere that involves south. The problem is the solution as far as I can tell is to just tell south to shut the gently caress up about it.

As per the manual;-

quote:

SOUTH_MIGRATION_MODULES = {
'cms': 'ignore',
}

Which leads to this..
code:
etc etc etc ....
   self.instances[app_label] = super(MigrationsMetaclass, self).__call__(app_label_to_app_module(app_label), **kwds)
  File "/Library/Python/2.6/site-packages/south/migration/base.py", line 88, in __init__
    self.set_application(application, force_creation, verbose_creation)
  File "/Library/Python/2.6/site-packages/south/migration/base.py", line 160, in set_application
    raise exceptions.NoMigrations(application)
south.exceptions.NoMigrations: Application '<module 'cms' from '/Library/Python/2.6/site-packages/cms/__init__.pyc'>' has no migrations.
:bravo:

Is there something that I *should* be using that just says

SOUTH_PLEASE_SHUT_THE_FUCK_UP_ABOUT = ( 'cms' )

Souths a great idea and all that but it gets into horrifying knots with off-the-shelf modules, and I really suspect its a pretty dangerous tool to use with mission-critical data, particularly when combined with postgres's seemingly endless capacity to light itself on fire.

Yay
Aug 4, 2007

duck monster posted:

I'm having a hell of a time with South (yet again! I'm becoming very unclear as to the benefit of this loving thing vs just manually tracking database changes when its so god drat fragile and prone to exploding) convincing it to shut the gently caress up about django-cms. The thing seems to have some very broken migrations and whilst it seems it'll install fine with a syncdb, it just vomits blood anywhere that involves south.

I've used django-cms a whole bunch, and only once had a problem with migrations, where south couldn't understand one, either because the version of south stopped supporting the declaration style, or because of a regression in django-cms (which would seem odd, because why would you recreate a migration?)

What errors are you getting trying to migrate the cms? And are you using the latest releases of South/django-CMS?

duck monster
Dec 15, 2004

Yay posted:

What errors are you getting trying to migrate the cms? And are you using the latest releases of South/django-CMS?

Yes, pip'd within the week.

One of the sub apps (content type or whatever it is) complains about a null value in content type id , and , uh, some crap about a missing relationship.

I cant even find where cms stores its migration files :(

For now I'm just going to nuke south and start again from the current state. I dont have time to go micromanaging souths wierd temper tantrums, especially when it loving ignores my request not to manage the cms app.

Yay
Aug 4, 2007

duck monster posted:

One of the sub apps (content type or whatever it is) complains about a null value in content type id , and , uh, some crap about a missing relationship.
ContentType is part of django, not django-cms. I think the CMS depends on it for permissions, but I can't think of anything else that uses it. Migrations are in the default place (which is described in south, at the very least, where SOUTH_MIGRATION_MODULES is documented) of cms/migrations, and also in cms/plugins/plugintype/migrations, as those are separate applications bundled in the module. South stores a representation of the ContentType model in its declarations, so that they continue to work should django ever modify them, I presume.

Is it asking you about a stale content type, possibly?

Hanpan
Dec 5, 2004

Cross posting from Stack Overflow, hoping some goon here has a smart solution for me.

I need to upload and attach an arbitrary number of images to my model. The site is for an estate agent, so they want to attach images to the property pages of their site. I can only think of the following solutions:

Create another model for the images and associate them using a ManyToMany field. There are many reasons why I don't want to do this, mainly because it confuses the process of adding a new property (they'd have to go two different admin screens) and since there could be thousands of properties, finding them in the ManyToMany drop down would be a real pain.

Create a rather complicated custom widget which launches a popup with a image model. The problem here is that I need to ensure that the images are linked to the property once it has been saved, so I need to figure out a way of doing that (since when you add a new property, there is nothing to create a link to.) I know I could do this with signals, but I'm unsure as to how I'd do this with multiple images since there could be more than one value to associate.

Does anyone have any suggestions as to how I can do this? Fundamentally I just need a way of uploading a dynamic number of images and somehow associating them with my model.

leterip
Aug 25, 2004

Hanpan posted:

Cross posting from Stack Overflow, hoping some goon here has a smart solution for me.

I need to upload and attach an arbitrary number of images to my model. The site is for an estate agent, so they want to attach images to the property pages of their site. I can only think of the following solutions:

Create another model for the images and associate them using a ManyToMany field. There are many reasons why I don't want to do this, mainly because it confuses the process of adding a new property (they'd have to go two different admin screens) and since there could be thousands of properties, finding them in the ManyToMany drop down would be a real pain.

Create a rather complicated custom widget which launches a popup with a image model. The problem here is that I need to ensure that the images are linked to the property once it has been saved, so I need to figure out a way of doing that (since when you add a new property, there is nothing to create a link to.) I know I could do this with signals, but I'm unsure as to how I'd do this with multiple images since there could be more than one value to associate.

Does anyone have any suggestions as to how I can do this? Fundamentally I just need a way of uploading a dynamic number of images and somehow associating them with my model.

I would guess that just adding an image model with a ManyToMany field, and then using custom widgets/forms to make it display how you would like is the best solution. There's no reason why you have to use the default drop down menu or have multiple admin screens.

MonkeyMaker
May 22, 2006

What's your poison, sir?

Hanpan posted:

I need to upload and attach an arbitrary number of images to my model. The site is for an estate agent, so they want to attach images to the property pages of their site. I can only think of the following solutions:

Create another model for the images and associate them using a ManyToMany field. There are many reasons why I don't want to do this, mainly because it confuses the process of adding a new property (they'd have to go two different admin screens) and since there could be thousands of properties, finding them in the ManyToMany drop down would be a real pain.

This is pretty much exactly what the admin inlines do. Make your M2M model that's linked back to the property, set up an admin inline for it, and you'll have a new section at the bottom of your property edit page where they can upload new images. There'll even be a delete button for deleting the linked image models. If you need to provide ordering, alt text, etc, you'll want to use a custom through model, which will make it a bit less clean.

Hanpan
Dec 5, 2004

Yea, admin inlines is what I needed. I feel like a loving fool for not knowing about these sooner.

MonkeyMaker
May 22, 2006

What's your poison, sir?

Hanpan posted:

Yea, admin inlines is what I needed. I feel like a loving fool for not knowing about these sooner.

The great thing about them is that they're just another form, so you can do anything to them you'd want to do to any other admin form (replace widgets with previews, etc).

Profane Obituary!
May 19, 2009

This Motherfucker is Dead
If I recall there used to be an issue with one of the migrations that django-cms had and sqlite. If you are using sqlite this could be your problem.

xPanda
Feb 6, 2003

Was that me or the door?
This might be too inspecific a question for this thread, but here goes.

Just this week I decided I wanted to learn more about web development and web applications, and as I've been using Python for a couple of years it seemed appropriate to learn Django. I went through the tutorial on the official page, and am looking at djangobook and some other tutorials on other sites. It's all making sense, and I'm looking forward to making my own programs.

What I'm curious about is presentation of the sites you make. All the tutorials have simple examples of HTML layout in the templates they have you make, but never really go into much depth about it and its really just there so you can see the results of the views they have you create.

The more I look into it, it seems that web development and web design are decoupled, and Django understandably is structured with this in mind. Django appears to cater for the development side, and nothing relating to Django touches much on the design side. What I've learned so far has been through looking at the admin templates/CSS files in the Django distribution and Inspecting Elements in Chrome.

So my questions are: is my observation about Django not involving itself with design correct, and; where should I go to learn how to make pages which don't look like they are from 1993? I expect there are a bunch of tools out there to help with layout and CSS design.

EDIT: is it kosher to copy/use the admin interface's styling and layout?

xPanda fucked around with this message at 05:39 on May 11, 2011

MonkeyMaker
May 22, 2006

What's your poison, sir?

xPanda posted:

So my questions are: is my observation about Django not involving itself with design correct, and; where should I go to learn how to make pages which don't look like they are from 1993? I expect there are a bunch of tools out there to help with layout and CSS design.

EDIT: is it kosher to copy/use the admin interface's styling and layout?

Yes, design and development are *relatively* decoupled. They do influence each other but not anything approaching massive amounts and could be handled on their own. It's a bit of the whole "form follows function/function follows form" debate, I guess.

I don't see any problem with copying the admin but I'd probably put some sort of disclaimer stating that you used their design, etc.

As for learning web design, read sites like A List Apart and check out Forrst and the like. I can get you an invite to Forrst if you need it, btw (or anyone else here for that matter). A lot of it will come from you developing a designer's eye for details, which is something you can't learn solely from reading books/web sites.

epswing
Nov 4, 2003

Soiled Meat

MonkeyMaker posted:

I can get you an invite to Forrst if you need it, btw (or anyone else here for that matter).

PM sent for one of these... :allears:

epswing fucked around with this message at 18:15 on May 11, 2011

Adbot
ADBOT LOVES YOU

xPanda
Feb 6, 2003

Was that me or the door?

MonkeyMaker posted:

I can get you an invite to Forrst if you need it

Sounds great! Sending PM.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply