Re: Problems saving model objects
Would you post the model and the relevant parts of the form and/or view? In any case, you can easily fix it by following the error message's suggestion of str(your_float). But if you think there's a discrepancy between the way your fields are behaving, post some code and we'll see if we can figure it out. Shawn --~--~-~--~~~---~--~~ 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: Getting the name of a matched URL pattern
On Jul 1, 2009, at 9:56 PM, Nick Fishman wrote: > > Hey everyone, > > I'm working with Django's named URL patterns, and was wondering how to > fetch the name of the URL pattern that triggered the view. For > example, with the following urlpatterns > > urlpatterns = patterns('', >url(r'^archive/(\d{4})/$', archive, name="full-archive"), >url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}, > "arch-summary"), > ) > > is there any way to fetch the name="full-archive" and > name="arch-summary" parameter from inside the view? > > I found a thread that talks about this same issue > (http://stackoverflow.com/questions/1042211/get-name-for-matched-url-pattern > ), > but the solution requires duplicating the URL pattern name in a > dictionary. > > Thanks, > > Nick Can you get what you need within the view by using request.build_absolute_uri()? --~--~-~--~~~---~--~~ 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: Sending JSON response from views
> > > The question, how can I return username of user in fields. I want > the JSON response to contain value user.username instead of id of > user oject > > Thanks, > Oleg > Get everything you want into a dictionary. Then something like this will give you what you need: return safestring.mark_safe(simplejson.dumps(return_vals)) If you want to return JSON to an AJAX call, then this will work: return HttpResponse(your_json_object, mimetype="application/json") --~--~-~--~~~---~--~~ 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: Add extra field to ModelForm
On Jul 11, 2009, at 6:23 PM, adelaide_mike wrote: > > Thanks Alex. > > A simpler way out would be if the agent field, which is a foreign key > pick list, could be caused to show only those values I determine > dynamically. > > Is this possible in a ModelForm? > > Mke If I understand correctly, what would help you is to add that 'agent' field to the exclude tuple of your model form and add another ChoiceField, setting its 'choices' dynamically in the __init__ of your ModelForm. Something vaguely like this: class ListingForm(forms.ModelForm): agent_id = forms.ChoiceField() def __init__(self, *args, **kwargs): realtor = kwargs.pop('realtor') super(ListingForm, self).__init__(*args, **kwargs) agent_choices = [('', '--SELECT AGENT--')] + [(str(x.id), x.name) for x in realtor.agent_set.all()] self.fields['agent_id'].choices = agent_choices class Meta: model = Listing exclude = ('agent',) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
unittest DB questions
My Django app uses postgres. I've defined TEST_DATABASE_NAME in settings and created that database in postgres. However, I haven't given my Django DB user permission to create and destroy databases -- just to own the database. So, I can't run manage.py test, because it fails to delete the database. Is there an easy way to tell manage.py to use an in-memory sqlite3 database instead? Or, even better, how do I get around this issue without giving my Django DB user postgres admin privileges? I'd (obviously) rather that my tests run with the same database backend as the real deal. Is it just pointless to disallow my Django db user from administering the postgres installation, since the only thing valuable on it is the live site data anyway? Thanks, Shawn --~--~-~--~~~---~--~~ 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: unittest DB questions
>> > > I highly highly recommend that you separate your development, testing, > and production instances of your DB and web server. You should never > ever develop or test on a production system! > > - -- > Randy Barlow That's already the case. I guess I wasn't clear enough. All the tests will only be run in the development environment, never on the test server. I guess I've been too stuck in the mindset that development and production systems should be as identical as possible, and just making my Django db login a postgres admin in test can't hurt anything. I just didn't want to make that user a postgres admin on the production server, and now I realize that there's nothing forcing me to do so. Thanks, Shawn --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
DjangoCon 09 Recording (was: Re: DjangoCon '09)
I've been listening to a lot of podcasts from PyCon 09, and for the most part the audio is horrible to the point of not being listenable. Are there any plans in place to do any recording at DjangoCon yet? It's vital that the actual speaker is miked. If someone is recording, even from a few feet away, the sound is crap. If there's no plan in place, then I'd be interested in helping out. I can bring at least one mic, and record at least the sessions I'm in, if it's okay with the organizers. Also, if PyCon 09 is any indicator, most speakers have no experience talking on-mic, so I'd give them a few tips. One of which is to repeat a question before answering it, because often the questioner doesn't make it onto the recording. I was also thinking about getting another portable recorder, like the Zoom H-2, for recording events. If we can even get one of those onto each podium, I think we could have awesome sound quality. So, if there's already something planned, please let me know. If not, let's see if we can hack something together so that everyone can benefit from an awesome event. Shawn Milochik sh...@milochik.com --~--~-~--~~~---~--~~ 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: I need to get psycopg2 installed on Mac OS X 10.5
I had the same terrors. Unfortunately I didn't document exactly how I did it, but my best recollection is that this will work: 1. Update setuptools: sudo easy_install -U setuptools 2. Download and unzip the tarball for psycopg2. 3. Edit the config file for psycopg2 as directed on other Web sites, paying special attention to the pg_config argument (make sure it points to the right place in your /Library/PostgreSQL folder). 4. Run: sudo python setup.py install --~--~-~--~~~---~--~~ 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: Way to show Progress bar in django while processing to send emails
On Jul 14, 2009, at 2:51 AM, gganesh wrote: > > Hi friends, > It could be helpful if someone suggest me to find a way to show > Progress Bar while django is processing to send mails .I have a form > where list of email Id's are displayed with checkbox and user selects > those and submits.Here i need to show a progress bar till django > finished its process of sending mails .is there any way to achieve > this . > > AJAX is a good solution. When the user hits the submit button, have JavaScript (I recommend jQuery) cancel the actual page submission and instead send the POST request. If I understand what you're saying, you want to do something for some number of checkboxes on the page. In which case, you could write a function which accepts a value (e-mail address, maybe?), then write a JavaScript loop to iterate through the checkboxes, count the number to process, then call your function repeatedly in the loop, updating a status bar based on how many have been completed so far. As for the status bar, check docs.jquery.com or the docs for jQueryUI to see if there's a pre-made one. I'm pretty sure there is. Worst case, you get a thin graphic and dynamically change its width to a percentage of the div it's in or something. Shawn --~--~-~--~~~---~--~~ 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: Database Model Layout Advice
I think you'd just have the interview table without any foreign keys, then add a ManyToMany to each of your Author and Illustrator models. You would still, of course, be able to start with your interview table and get the authors or illustrators, if necessary in your app. It might be handy to set the related_name property of this field for easy reference from your Interview model. Shawn --~--~-~--~~~---~--~~ 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: Database Model Layout Advice
On Jul 14, 2009, at 9:23 AM, The Danny Bos wrote: > > > So the Author and Illustrator tables would stay as is. > How would the Interview table talk to both of those tables at once, to > allow me to assign an interview to who I'm interviewing. Whether it's > an Illustrator, Author or even down the track a Publisher. > > Like so? > > class Interview(models.Model): >body = models.TextField() >publication_date = models.DateField(auto_now=True) > >interview_subject = ... > >class Meta: >ordering = ['publication_date'] > > No, the other way around. The interview table wouldn't have anything in it to point to the illustrator or author. The Author and Illustrator models would each have a ManyToMany field defined to the Interview model. The "related_name" property of this field will give you an easy way, when using an Interview model, to pull its related Author or Illustrator objects. When you start with an Interview object, you'll need to do something like check to see if: len(this_interview.author.all()) > 0 Shawn --~--~-~--~~~---~--~~ 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: Count from 1 to 50
On Jul 14, 2009, at 10:27 AM, The Danny Bos wrote: > > Is there a way to create a simple list using Django to go from 1 to > 50. > Eg: > > 1, 2, 3, 4, 5 ... 49, 50 > > I figured it'd be easy, but I can't get it. > I tried ... > > {% for i = 1 in 10 %} > {{ i }}, > {% endfor %} > > No go. There may be a better way, but why not just pass a range in the context? context = {'numbers': range(1,51)} Then you can do: {% for num in numbers %} ... --~--~-~--~~~---~--~~ 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: Database Model Layout Advice
On Jul 14, 2009, at 10:35 AM, Jonathan Buchanan wrote: > > One option would be to use a generic relation: > > http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1 > > Regards, > Jonathan. > > Yes, this is probably the right way to do it. I had heard about generic relation, but forgot. Shawn --~--~-~--~~~---~--~~ 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: newbie question information about js
On Jul 14, 2009, at 12:30 PM, luca72 wrote: > > Hello > I have a question in my view fiole i have this > > def myfunct() > do this... > now i need only to return a js alert message on the same page > is it possible? We need more information. Are you calling myfunct() via an AJAX call? I don't see a request in the argument list, but is that because you left it out, or is this not a "view" function (receives request and returns a response)? Your function executes server-side, and JavaScript (obviously) executes client-side. Python can't do the alert box, but it can: 1. Render a template containing the JavaScript. 2. Pass the JavaScript text into a templatt, via the Context. 3. Return a JSON value via AJAX to the template's JavaScript (you have to bind the handler in the JavaScript, preferably using jQuery for ease-of-use) that passes the message to be used for the alert. I hope this helps. If not, please send more relevant detail. Pretend you're explaining the problem to someone who has no idea what you're talking about, because we don't. Shawn --~--~-~--~~~---~--~~ 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: How to store user
On Jul 14, 2009, at 2:50 PM, Viktor Semykin wrote: > > Hi everyone. > > I use django.views.generic.create_update.create_object to allow users > to post new entries on my site. > How can I make model automatically save the user currently logged in? > Model like > class MyModel(Model): >user = ForeignKey(User) > --~--~-~--~~~---~--~~ Set user = request.user in your view. A slightly more "automatic" way of doing it would be to override the save method of your model, but I think you'd still have to pass the user instance into it. --~--~-~--~~~---~--~~ 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: multi applications
On Jul 14, 2009, at 2:57 PM, sjtirtha wrote: > Hi, > > I'm looking for documentation, which describes how to build a web > application which includes many other applications. > For example like Facebook, it has following applications on the > first screen: > 1. On the left side is dynamic menu > 2. In the middle is the microblogging > 3. On the right side on the top is Request block > 4. On the right side in the middle is Highlights > > How can I build a web application like this? I was thinking to have > a main application which has a template that includes all other > applications. > But how can I set the context specific for every templates of the > included applications? > > regards, > > Steve There are several different aspects to what you are asking for and describing. The first part is showing all the apps within one page, and that's just a matter of a well-formatted CSS/HTML template. This template may (should?) include smaller templates (for each section). Ultimately your page will be called by a single URL, so you'll receive the entire request in one place. From there, you will have to decide how to handle the contexts. Look into the RequestContext class. The things you need to display on all pages, regardless of what the "main" view is at the moment, can be moved over there to avoid clutter. The "main" content could be handled in the view called directly by urls.py, and the rest can be inherited from the context dictionary keys coming from your request contexts. Also, you'll probably want/need to handle all the smaller apps on the page with AJAX, because you won't want to refresh the entire page for updating the microblog, etc. And since you're referring to FaceBook, they even do most of the processing of actions in the "main" section with AJAX, just to have a smoother user experience. These are certainly vague answers. If this isn't what you're asking for, please provide more detail. Shawn --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Basic AJAX (was: Re: multi applications)
On Jul 14, 2009, at 3:43 PM, sjtirtha wrote: > Thank you for the explanation. > This helps me very much. > > You mentions about handling the smaller apps on the page with AJAX. > Is there any basic functionality to do this from Django Framework? > Or do you have any ajax lib/framework as preference? > > Steve Use jQuery -- it'll automate most of it. Here are two simple samples. Both are in the $(document).ready() function. Once you use jQuery for about three minutes, you'll know what that is. This does a GET request to get a user's current balance. It then writes the balance (returned as JSON by the view) into the HTML div with the id current_balance. $.get("/balance_json/{{ch.slug}}", {}, function (data){ $('#current_balance').html("$" + data.balance); }, 'json'); This does a POST to submit a form to send an SMS message. The view also returns JSON in this case, (created from a Python dictionary), which will have a key of 'success' with a value of the success message, or a key of 'failure' with an error message in it. $("#sms_form").submit(function(event){ event.preventDefault(); // stop the form from submitting and refreshing the page var data = $("#sms_form").serialize() // now send the data to the server via AJAX $.post("/patient/{{ch.slug}}/sms/", data, function(resp){ if(resp.success) { $("#messages").html("" + resp.success + ""); $("#success-message").animate({ backgroundColor: "#E6EFC2" }, 2000); update_activity(); } else if (resp.fail) { $("#messages").html("" + resp.fail + ""); $("#fail-message").animate({ backgroundColor: "#fbe3e4" }, 2000); } }, "json"); }); I don't know if there are are super-special Django tools to do AJAX, but as it's JavaScript and Django's Python, I doubt it. The most useful things in Django are simplejson and safestring (both in django.utils). You'll need simplejson to easily dump a dictionary into a JSON string to pass back to JavaScript, and you'll need safestring to "mark_safe" JSON strings you're going to pass via the context to be rendered within the templates. Otherwise they'll be escaped automatically for your protection. Shawn --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Deployment Tool Recommendation
I need to set up a one-click deployment from development to production. What tool(s) are currently best-of-breed in the Django community? Thanks in advance. The tool must be able to automate the following manual process: 1. Log into the production server via ssh, using a user ID which requires a password (no public key encryption). 2. Run 'sudo bash' then 'su - otheruser,' where otheruser is the ID under which Django runs. I can't log in via ssh directly as this user for security reasons, and they have no password, so I can't directly su to that account. 3. As 'otheruser': killall python cd $SITE ./manage.py migrate ./manage.py runfcgi host=127.0.0.1 port=8100 -- settings=companyname.web.sitename.settings ./manage.py runfcgi host=127.0.0.1 port=8200 -- settings=companyname.web.othersite.settings ./manage.py runfcgi host=127.0.0.1 port=8300 -- settings=companyname.web.coolsite.settings ./manage.py runfcgi host=127.0.0.1 port=8400 -- settings=companyname.web.funsite.settings 4. (Possibly optional but highly desirable): kill and restart nginx, which requires satisfying the prompts for the passkeys for each SSL certificate we use. --~--~-~--~~~---~--~~ 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: Deployment Tool Recommendation
> > Any reason why you can't do 'sudo -u otheruser bash -l' or even > 'sudo su > - otheruser'? Seems strange to be able to sudo to root, but unable to > sudo to a role account. No, I don't see a reason not to do it that way. I just can't directly su to the other user due to the lack of a password. But those options (while syntactically different) are functionally identical. Probably better than they way I'm doing it though, because there are fewer steps. --~--~-~--~~~---~--~~ 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: New to unit testing, need advice
Basically, you want to test anything that might break if another part of the code is changed that interacts with it, might break if the application takes an alternative flow (maybe finally hits the 'else' of that if/else statement), or could possibly receive invalid input. Unfortunately, that is theoretically everything. I don't know what the correct answer is, and I suspect that the correct answer is (as is so often the case) "it depends." All applications are unique. In any case, if you care enough to be doing it at all, and are concerned about doing it the right way, then you'll probably be fine. --~--~-~--~~~---~--~~ 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: syncdb doesn't updated database after adding null=True to an IntegerField
> > Apart from django-evolution, also take a look at http://south.aeracode.org/ I second that -- I use South and it's really handy. I couldn't get Evolution to work (maybe I didn't wait long enough ^_^), but South does everything I need. The nice thing about it is that it's easily reversible or completely removable if you need to dump it -- just delete one database table and remove it from installed_apps. Shawn --~--~-~--~~~---~--~~ 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: Generating forms question.
On Jul 16, 2009, at 5:16 AM, Viacheslav Chumushuk wrote: > > class Book(models.Model): >author = models.ForeignKey(Author) >title = models.CharField(max_length=100) > > All that I need is edit some book by primary key. Just make a forms.ModelForm for your Book model. The class Meta will contain 'model = Book.' You don't need any of that inline stuff to edit a single instance. Briefly (assuming your ModelForm is named BookForm): In your view, you will make an instance of your ModelForm: book_form = BookForm() Then do the typical render_to_response bit. You will also have an if request.POST in your view. If that's hit, then you will declare your form like this: book_form = BookForm(request.POST) #if you're not doing a request.POST.copy(), which you probably should. Then: if book_form.is_valid(), you save it. If not, you let it fall out of that if statement into your render_to_response part. Make sure that if the form doesn't validate, you're not doing the "bare" book_form = BookForm() and overwriting your bound version. The bare version could be above the if, or in an else clause. Shawn --~--~-~--~~~---~--~~ 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: Accessing Form Data in Form Object
Try using form.data.initial to get the text only. --~--~-~--~~~---~--~~ 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: question about the DEBUG setting
And as a brief aside, I've found it helpful to have a local_settings.py, which lives outside version control. If you put your database stuff and DEBUG flag there, you never have to fix it when you move to production. --~--~-~--~~~---~--~~ 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: Non-editable (but visible) fields in admin?
On Jul 16, 2009, at 11:11 PM, Rubens Altimari wrote: > > Hello, > > Is there a way to show model fields in admin, but prevent them from > being edited? Using 'editable=False' in the model won't do, since then > the field simply is not displayed. > > Also, on a related topic: is there a simple way to allow the user to > set a certain field when *creating* a new record, but then "lock" the > field from further changes? Similar to the previous question, only I > wanted to allow the field to be set when creating the record. > > Thanks in advance, > Rubens > The short answer to part one is "yes," because you can customize everything in Django. However, I don't know if it's trivial to modify the admin in that way, and that's really not the kind of thing the admin was intended for. You should probably just make a form for this functionality. It would take about a minute, and the view wouldn't take much longer. For the second part, certainly. Just override the save function of the model. If the object already has a primary key, then don't save those fields. There may be a more elegant way to check whether it's already been saved than checking the instance.id, so maybe someone else will chime in. Shawn --~--~-~--~~~---~--~~ 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: Limiting administration based on user settings
On Jul 17, 2009, at 8:08 AM, Rune Bromer wrote: > > Hello. > > For a small webshop I have the following models: Product, Category, > Country and UserProfile. Each user have a country set with a FK, and > should only be allowed to see content in the administration assigned > to the same Country. The Product and Category have FKs to the Country > model so I can ensure this by writing a "formfield_for_foreignkey" on > the ModelAdmin. So far so good, as I can also filter the admin list > view by overwriting the queryset() model on the ModelAdmin. > > The problem now is that I can assign several categories to each > Product. But I only want to allow people to see the Categories for the > same Country as the product. How is that possible. Apparently the > options for the m2m field is not filtered by the queryset and I can't > see there is a formfield_for_manytomanyfield? There is probably a way to do this if you dig deep enough into the Django code, but the admin wasn't meant for this. According to the second edition of "The Definitive Guide," the admin was meant to be an easier-to-use and less-powerful replacement for the command-line interface to your database. If you are developing in an environment where the content creators are trusted to have this kind of access, then it's an added convenience. Creating forms and modelforms is simple, and have the added benefit of being much easier to control layout. Your views could get the request.user.country and use that as an automatic filter. Shawn --~--~-~--~~~---~--~~ 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: how to alter (add/del cols) the tables from the existing models
Look at south or evolution. I use south, personally. It does exactly this very easily. http://south.aeracode.org/wiki/Tutorial1 To add a column to the database with south, you add it to the model, then: ./manage.py startmigration appname migration_name --add-field model.field So, if your app was named awesome, and you added field thumb to model Hand, it would look like this: ./manage.py startmigration awesome added_thumb --add-field Hand.thumb then: ./manage.py migrate Of course, check out the tutorial. --~--~-~--~~~---~--~~ 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: Current user in model.save() context
I think you still have to pass the user, but you can tell if the item is modified or new by whether its id has a value. If it's brand new and has never been saved, then it won't have one. --~--~-~--~~~---~--~~ 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: looking for a clean, easy way to force user to change password on first login
This isn't ideal, but it should work (or prompt someone to propose something better). Add a one-to-one table for the user, with a field containing the initial password (encrypted, of course, so it looks like the password in the auth_user table). When the user logs in, have the login page check to see if the passwords match. If they do, redirect to the password change page instead of the normal redirect page. --~--~-~--~~~---~--~~ 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: Graphing (Pie Chart, Bar Graphs, Line Plots, etc) in Django?
"Beginning Python Visualization" was recently published to fit exactly this need. (No disclaimer -- I have no association with the book other than having purchased it.) --~--~-~--~~~---~--~~ 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: looking for a clean, easy way to force user to change password on first login
If I understand correctly, you can do this without having to re- implementing the login. You will have to make a (very simple) login template, and write a login view that contains these: from django.contrib.auth import login login(request, form.get_user()) Then you can handle the redirection however you like. --~--~-~--~~~---~--~~ 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: Don't understand ModelForm
On Jul 21, 2009, at 12:00 PM, mettwoch wrote: > > Hi, > > I'd like to implement a simple "create" & "update" form for my > "Partner" model using ModelForm. How can I make the difference in a > view "save" function whether the POST comes from a "creation" or an > "update" form? Unfortunately the primary-key seems never to be > included by default in the form fields! This would have made it easy. > > Got 3 view functions: > new(request): > > > edit(request, partner): > > > save(request): >update partner >else: >create partner > > Many thanks for any hint > Marc > > > You don't need the save version. Your edit form will save the object instance whether it's new or not, with the .save() method. This is one of the nice things about Django models -- it simplifies things. If you need to do something differently depending on whether the object is being saved for the first time, you do it by overriding the save() method in the model itself. Shawn --~--~-~--~~~---~--~~ 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: Don't understand ModelForm
On Jul 21, 2009, at 12:49 PM, mettwoch wrote: > > Sorry my old brain doesn't get it. Is there somewhere a complete CRUD > example using ModelForm? > > Thanks > Marc CRUD: If your modelform is called PartnerForm, then. CREATE and UPDATE: #on initial page load (no POST) #if you have a partner object already (named partner_instance): partner_form = PartnerForm(instance = partner_instance) #otherwise: partner_form = PartnerForm() #in the POST portion of your view partner_form = PartnerForm(request.POST) if partner_form.is_valid(): partner_form.save() DELETE: #if you have a partner object named partner_instance partner_instance.delete() READ: What do you mean? Partner.objects.get(pk = 12) #or whatever #or, from the modelform instance, assuming #something like partner_form = PartnerForm(instance = partner_instance) #has happened: if partner_form.is_valid(): partner_name = partner_form.cleaned_data['name'] I hope this helps. If not, please be more specific. --~--~-~--~~~---~--~~ 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: Recommendations for a Django development book?
"The Definitive Guide to Django," second edition. It was just published and was written by Adrian Holovaty and Jacob Kaplan-Moss. In case you don't know who they are, they're the co-creator and a lead developer of Django, respectively. --~--~-~--~~~---~--~~ 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: Don't understand ModelForm
> > This always creates a new partner! In fact, in the ModelForm > documentation is written that the save() method creates a new instance > unless an existing instance is passed to the ModelForm constructor. So > one has to differentiate if the POST comes from an initially empty > form or from a bound form! How can this be done? I believe that it > could be easily achieved if the primary-key were always included as a > hidden field. If it's empty, then 'create' otherwise 'get' & 'update'. > >> >> DELETE: >> #if you have a partner object named partner_instance >> partner_instance.delete() >> >> READ: >> What do you mean? >> Partner.objects.get(pk = 12) #or whatever You are right, I made a mistake there. I typed it off of the top of my head, instead of copying and pasting from working code. All you have to do is what I did above in the create -- have an if statement. If the partner already exists, you have to get that object (using the Partner.objects.get(your_criteria_here) and pass it using the 'instance' keyword. Something like: #if it's an existing partner: partner_form = PartnerForm(instance = partner_instance) #else: partner_form = PartnerForm(request.POST) --~--~-~--~~~---~--~~ 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: Don't understand ModelForm
On Jul 21, 2009, at 3:39 PM, mettwoch wrote: > > Sorry to insist, but that's what I don't understand here. How can I > know that the partner that is 'posted' is new or not? I really become > crazy here. I can't believe after all the good things I've discovered > in Django that this can be so hard. > Okay, thanks for clarifying. Well, you can have the partner identified in the URL by a slug or something, for one. That's probably the easiest way. You do need to track it somehow, starting when you pull the instance from the database to populate the ModelForm. Otherwise, how can you know that it's someone new versus someone with the same details? The way I'm doing it is by having separate views and urls for editing and creating a new entry. But in any case you have to write something somewhere when you are pulling an existing record. --~--~-~--~~~---~--~~ 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: Don't understand ModelForm
On Jul 21, 2009, at 4:22 PM, Dan Harris wrote: > > What do you mean by having the ID "in" the form? I don't quite > understand what you are trying to do. > > On Jul 21, 4:15 pm, mettwochwrote: >> Ok, I come back to what I wrote before. If the partner already exists >> it has an id (primary-key or whatever). If it doesn't exist it has no >> id. I'd just like to have the id in the form. Is it a bug, is >> something missing here or am I completely on the wrong track. That's >> basic database form handling. Well I could fallback to using Form >> instead of ModelForm and maybe I'll manage to get that id in the form >> as a hidden field or whatever, but I wonder how such basic things >> seem >> impossible with the ModelForm. >> >> Thanks for Your patience >> >> Marc >> What he's referring to is the fact that, if you're using a ModelForm, the model's ID is not available in the template. And my answer to the question is that I use the ID (or a slug) in both the URL and in the form's "action" argument, so that when it's submitted, you have the info you need in the request. Just as a quick example: (r'^patient/(?P[-\w]+)/$', 'view'), This will pass the variable "slug" to the view, and the slug is used to identify the patient. If you don't use slugs, the ID works just as well (although it makes for less friendly- looking URLs). --~--~-~--~~~---~--~~ 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: Don't understand ModelForm
To expand on what Dan said with a full (tested and working) example: In urls.py: (r'^partner/$', 'partner_page'), (r'^partner/(?P\d+)/$', 'partner_page'), In the form tag of your template: The view: @login_required def partner_page(request, partner_id = None): if partner_id: #existing partner partner_instance = get_object_or_404(partner, pk = partner_id) else: #new partner partner_form = partnerForm() partner_instance = None if request.POST: post_data = request.POST.copy() partner_form = partnerForm(post_data, instance = partner_instance) if partner_form.is_valid(): the_partner = partner_form.save() partner_id = the_partner.id template_file = 'partner.html' context = { 'partner_form': partner_form, 'partner_id': partner_id, } return render_to_response(template_file, context, RequestContext(request)) --~--~-~--~~~---~--~~ 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: best approaches with render_to_response and large forms
On Jul 21, 2009, at 5:21 PM, Juan Hernandez wrote: > Hi there, > > i have two questions in my mind. > > 1- Let's say that I have tens of views. Do I always have to use > render_to_response? I find it totally against DRY. My solution was > creating a wrapper that would take as arguments the template file > name and a dictionary with all the parameters for that template and > return the render_to_response object. is there any other approach? > > 2- Let's say that I have a very big form. Something like a long > report that for some weird or bureaucratic reason can't be cut into > easier pieces to fill. Is there any way where I could simply check > every field for cleaned_data['xxx'] at once instead of writing every > single line? I am doing a list and then a map() against it but I > feel I'm not using the best practice here... any thoughts? > > Thanx a lot people > Juan #1. What do you mean -- tens of views that all populate the same template in the same response? If so, then you can have any number of functions that don't receive request objects and return response objects, and call them to do whatever you need. You might want to have a look at the RequestContext object, and the idea of putting a bunch of functions into one place and making use of the TEMPLATE_CONTEXT_PROCESSORS tuple in settings.py. #2. Can you do something like: for key, value in cleaned_data.items(): if value: pass #do something --~--~-~--~~~---~--~~ 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: passing variable to objects.get
Do you know for certain that indata contains a value before you try the objects.get()? If you are getting an error like "no value found," it sounds like you might not. If you're getting something like "no such object," then it would be a different problem. Is the field "word" in your model a unique value? Also, does your regex match the value for indata? Maybe there are punctuation marks that don't match \w. Also, once you've confirmed that indata contains a value, dump its type and value to the screen with the logging module or a print statement, and see if it looks like the kind of data your database should be expecting. If you don't get it to work, please post sample values for "word" in the database and sample values for "indata," including a couple of URL strings. Shawn --~--~-~--~~~---~--~~ 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: Automatic updating of open-high-low-close data
Why not just use the automatic "id" field as the primary key? --~--~-~--~~~---~--~~ 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: Slugs, why would you ever need to store one in the database?
My understanding of the slug field is that prettier URLs are the main point. "Slug" is a newspaper-industry term, and since Django has its roots there, it's now a Django term. Django does' in fact, contain a slugify function: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#slugify You can import and use this in a view, not just in a template: from django.template.defaultfilters import slugify Note that it does not enforce uniqueness; you'd have to do that yourself. We just append a number to the slug. So, for example, if you already had john_smith as a slug, you'd end up with john_smith2. We use unique slugs, and store them in the database. The main reason (I think) for storing it in the database is the fact that you can easily retrieve a model instance using (slug = value) in the objects.get(). Having the ID and the slug both passed in the URL is redundant. We use one or the other. Your use case may dictate a different approach, but it doesn't make sense to me to have two values in the URL, both of which should be referring to the exact same record. Shawn --~--~-~--~~~---~--~~ 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: creating a list of sql that is executed
Look for the post_save signal. Once you can attach your own function to that signal (make it listen for the signal and kick something off), you can probably have Django dump the raw SQL query that is created to a text file. I don't know the syntax for that, but check the django.db docs. Having said that, this seems like a really hacky workaround. I'd see if there's some "best practices" method of having distributed databases that can sync. I'm guessing it's not going to work with sqlite3, though. If it's a Django site, why aren't you hitting the same database from work that you are from home, anyway? I'd think you'd be using the same URL. The only exception is if you're doing some development at home in a test bed, but then the data you create wouldn't be "real" and shouldn't go into the production database. Shawn --~--~-~--~~~---~--~~ 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: How to add basic text formatting to the message body of send_mail in django?
On Aug 21, 2009, at 12:50 PM, Ray wrote: > > Hi, > I'm trying to do this: > > send_mail(subject='hey, Ray', >message='Hey ray, this is an automated message, \n heres a > link: yahoo.com or yahoo.com', >from_email='bl...@gmail.co', >recipient_list=['blan...@gmail.com'], >fail_silently=False >) > > I'm trying to add newline formatting to the text and perhaps a link. > Is that possible to do without having to send a html message? I tried > \n, that didn't work > > thanks, > Ray Try \r\n -- it's the newline standard used by mail servers, for some reason. Shawn --~--~-~--~~~---~--~~ 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: dump data using json
On Aug 21, 2009, at 11:39 AM, ashish tiwari wrote: > hi Friends > > i heared about the json format > django provided utility of dumpdata, loaddata which dumps and loads > data in json format. > > but i dont know,how to use it.. > > i want to dumpdata...from my app > > name of iFriends > name of People > > iFriends>People>models.py > > class Person. > > class Item > > so i want to dump all data which is present in class "Person" > how can i "dumpdata" from my app "class Person " in " .json " > fromat and "loaddata" > > > thank you... > > > - > ashish > Please be more clear about what you're trying to do. You can use simplejson to convert Python objects, such as lists and dictionaries, into JSON, and the reverse as well. However, you can't just dump a complex object such as a model to a JSON string. I ask for more explanation because you can't dump all data from "a class" anyway -- you'd have to instantiate the class (pull one record from the database) and dump data from it. Shawn --~--~-~--~~~---~--~~ 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: Model Forms
There are different options, depending on whether the instance of B is a required part of A's model. 1. You can add an "exclude" section to your ModelForm of A. Validate BForm, validate AForm, then take the resulting instance of A and set it's 'b' attribute to the resulting instance of BForm.save(). 2. Validate BForm first. Then, before AForm.is_valid(), you set AForm.data['B'] manually, to B.pk, then validate A. Maybe there's a more elegant way, but those will work. Shawn --~--~-~--~~~---~--~~ 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-usable/pluggable app form question.
I'm working on an existing project that I'm splitting up into multiple apps. My question is about how to use those apps together. Say I have an app called "main." Then two supporting apps which sit outside of that Django project folder. They are "foo" and "bar." They are not "installed" as Python modules -- they're just in another directory on the Python path, and they are added to the INSTALLED_APPS of the main project. Let's say foo has a template which is just a div containing a form.as_p tag, and the form it uses is also in the project foo. The function to act on that form's submission is in the views.py of foo. If I want to offer that form on my site from the main app, how do I do it? Is it possible to not import the forms, views, and templates from foo into main, and just use template tags to load the foo content into main and have it processed by foo? The point is to keep all foo logic outside of the main app, so main doesn't have to "know about" foo or any of its internals. This may not strictly be a "pluggable" app, but it works for our application. Is the way to do this documented anywhere? Thanks, Shawn --~--~-~--~~~---~--~~ 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: Reading text files into mysql database
On Oct 1, 2009, at 9:25 AM, Carlos Gustavo wrote: > > Hi All, > > I am a django newbie and so far it has been great. > > How do I populate a mysql database table with data from a text file? > > Also How do I dump results of a database query into a text file. > > I have already created my django model for the mysql table. > > Thanks in advance. > > -Carlos To populate the database with the contents of a text file, write a script that imports your Django model, open the file (perhaps using the csv module, if it's a csv file), and iterate through the file, creating new instances of your model and saving them. Here's a barebones example I made by taking something I actually use and cutting out most of it so it's clear: http://pastebin.com/f651cf8de As for dumping the output of a query to a text file, that depends. The first thing that springs to mind is to write a method in the model which dumps the data you want in the format you want. So, for example, if your model has a csv_format(self) method, you could do the queryset, then iterate through it, writing instance.csv_format() to a text file. Shawn --~--~-~--~~~---~--~~ 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: Two Formset on the same page
Are both Django forms being rendered in the same HTML form, or are they separate? If they're in separate HTML forms, that could be the problem. --~--~-~--~~~---~--~~ 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: How to pass values between views/templates
Your current approach makes sense. If you're concerned that you're storing too much data in the session, you could just maintain a session ID in the session and use that as a key for an sqlite3 database or something, or even a model in your app's database, if that makes sense. Is there any reason you're unsatisfied with the way you're doing it? Shawn --~--~-~--~~~---~--~~ 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: Extending Django's Flatpages
If you're trying to add template tags, check this out: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/ Your custom tags go in a directory named "templatetags" in your app directory. Shawn --~--~-~--~~~---~--~~ 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: How to pass values between views/templates
Remember that people often represent their best work, and I bet they only answer the questions they're most confident about their experience with. If you care that much about your code, you'll be fine -- once you have your 10,000 hours* in as a coder, people will be in awe of your code too. Shawn *See "Outliers" by Malcom Gladwell --~--~-~--~~~---~--~~ 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: iphone to website
On Oct 8, 2009, at 9:33 AM, grant neale wrote: > > Well 1 problem I was thinking about was uploading photos. > Any suggestion. > > Grant While it's not Django-specific, it's easy to write a Python script that monitors an e-mail address and downloads and stores the attachments. You could schedule this (maybe with cron) and your script could import your Django model and update the database with the info on the new files. --~--~-~--~~~---~--~~ 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: Problems with custom Auth Backends: 'NoneType' object has no attribute 'DoesNotExist'
The way to do what you're trying to do is to take advantage of the following: http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users In brief, you'll create another model, which will then be specified in your settings.py by AUTH_PROFILE_MODULE. From then on, you will be able to use the contrib.auth.model.User builtin function get_profile() to --~--~-~--~~~---~--~~ 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: iphone to website
On Oct 8, 2009, at 9:33 AM, grant neale wrote: > > Well 1 problem I was thinking about was uploading photos. > Any suggestion. > > Grant While it's not Django-specific, it's easy to write a Python script that monitors an e-mail address and downloads and stores the attachments. You could schedule this (maybe with cron) and your script could import your Django model and update the database with the info on the new files. --~--~-~--~~~---~--~~ 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: efficiently deleting content based on how old it is
Chris, I know this doesn't answer the hard part of your question, but here's a simple way I delete old stuff from a database via an external script that's run by cron: Item.objects.filter(date_updated__lte = datetime.now() - timedelta (days = 30)).delete() If you use timedelta, you can easily identify records that are too old. As for your complicated references, assuming you don't have cascading deletes happening automatically, you may have to do an objects.filter(), get the results, and iterate through those, deleting their dependencies before deleting them. That may be several levels deep. In any case, make sure you back up your database regularly in case of a mistake, and don't forget to add unit testing with robust fixtures to develop and test this purge feature. Shawn --~--~-~--~~~---~--~~ 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: Problems with custom Auth Backends: 'NoneType' object has no attribute 'DoesNotExist'
The way to do what you're trying to do is to take advantage of the following: http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users In brief, you'll create another model, which will then be specified in your settings.py by AUTH_PROFILE_MODULE. From then on, you will be able to use the contrib.auth.model.User builtin function get_profile () to return your custom object. You will not be subclassing the User model; there's no need, and it greatly complicates things to try. I'm currently working on doing this myself, because I am adding requirements for the password change. Namely, I need the password to expire after 90 days, I need the initial password to require a reset after the first successful login, and I need to enforce some rules about the complexity and non-reuse of the password. Shawn --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Middleware to replace @login_required
Middleware question: If 100% of an apps views require a logged-in user except for the login page, does it makes sense to have middleware check the URL and redirect to the login page rather than putting the @login_required decorator over all the views? I have to ensure that users' passwords expire, and to do that I need to check the user's profile on each page load and redirect to the password change page if it has. I tried doing this within a context processor, but redirection doesn't work from there. I was told on the IRC channel that I should be using middleware. If that's the case, doesn't it makes sense to just handle the (nearly identical) task of @login_required in the same middleware? Thanks, Shawn --~--~-~--~~~---~--~~ 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: Middleware to replace @login_required SOLVED, + PCI Compliance
Ethan, Thanks for the feedback. I did create my own middleware, and it was ridiculously simple. I just looked at the django.middleware files and saw how easy it was. I only had to make exceptions for the pages pertaining to resetting a forgotten password (from django.contrib.auth.views) and the login page. I put them in the same middleware, because it was so simple. I would tend to agree that they should be separate, except that they're each handling one end of an 'if' block (logged in or not), and it might actually make more sense to the maintenance programmer to have them together. For the curious, here's my code. Also, for the wise and helpful who might find fatal flaws and will help me correct them: http://pastebin.com/f4ddc98b6 Incidentally, this is part of a larger effort to make contrib.auth PCI compliant. Since this was the last step in that effort, I'm planning to write up the whole thing, since I asked about how to do it and didn't get any answers. PCI compliance requires password expiration after 90 days (max), a minimum password length, letters and numbers in the password, and disallowing users to re-use old passwords for a minimum of the four most recently used. Shawn --~--~-~--~~~---~--~~ 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: Regarding login functionality in page other than login
On Sat, Oct 17, 2009 at 1:42 PM, Raashid Malikwrote: > I have a custom page e.g home and i want to have login functionality on this > page. I m using django registration module for django login and > registrations process. Now i m totally blank on how to implement this. Plz > help > > > Thanks, > Raashid Malik > > Raashid: I think everything you need is here: http://docs.djangoproject.com/en/dev/topics/auth/#how-to-log-a-user-in You'll either use these functions, or import the login view from django.contrib.auth.views and maybe subclass the login form from django.contrib.auth.forms. Shawn --~--~-~--~~~---~--~~ 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: How to enforce uniqueness based on two model fields
Guillermo, Please see the 'unique_together' syntax: http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together --~--~-~--~~~---~--~~ 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: Add Request Parameters
Keith, Depending on what you're doing, it might be a good place to use a context processor. http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors Also, if you're using (or can use) the session middleware, you can store these values in the session. Shawn --~--~-~--~~~---~--~~ 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: foreign key to an abstract model
Here's how to do a generic foreign key: http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1 -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to show output from external process
On Dec 4, 2009, at 12:10 PM, Philippe Clérié wrote: > I am working on an application that requires getting some data offsite, > process it and load the site database. This process can take up to five > minutes. I would like to be able to capture the output and present it to the > user so he knows how far along he is and whatever errors he needs to know > about. > > I was thinking of using a page with HTTP Refresh and show the current output > of the process via a pipe. I'm not sure how feasible that is. Are there any > know solution to this type of situation? > > Thanks for any tip!! > > -- > > > Philippe > How about AJAX? You could have JavaScript that sends a request to one of your Django URLs, which has a view that returns the current status. You could than use JavaScript to update the status on the page without any reloading. I do something similar with a Web page that does an API lookup. The page loads, then an API call check's a users account balance. Once the result comes back, the balance is shown on the page. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to show output from external process
On Dec 4, 2009, at 1:34 PM, Philippe Clérié wrote: > Are you using a library or did you code it yourself? What would be a good > library to start with? > > -- > > > Philippe > I use jQuery, and that makes it simple. First, have a view that returns JSON. Basically, this is all you need in your view: #where return_data is a dictionary return HttpResponse(simplejson.dumps(return_data), mimetype="application/json") Then, you'd have JavaScript like this: #a GET version: //Note: The variable name 'data,' supplied in the anonymous //function definition below will receive the JSON sent by //the view. If you have a key in your returned dictionary //named 'balance,' you can refer to it with data.balance. $.get("/balance_json/{{ch.slug}}/", {}, function (data){ $('#current_balance').html("$" + data.balance); }, 'json'); #a POST version //Same as above -- the returned dictionary (serialized to JSON) //contains a key 'success' or 'fail.' In this case, the variable is //named resp instead of data. Blame my predecessor for the inconsistency. $.post("/patient/{{ch.slug}}/appointment/", data, function(resp){ if(resp.success) { $("#messages").html("" + resp.success + ""); } else if (resp.fail) { $("#messages").html("" + resp.fail + ""); } }, "json"); Also, if you've never used jQuery before, you should have a JavaScript section like this: $(document).ready(function(){ //put your code here -- this is the far superior replacement //for putting stuff in an 'onLoad' section. } One more thing. Since you want to do do this repeatedly, you should use a setTimeout() call at the end of your function so it happens again and again. You may put it within an 'if' check to see whether the JSON returned a value indicating that the process is complete. Like this: setTimeout('your_function_name_here()', 5000) //this is a five-second delay Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to create multiple 'submit' buttons using 'forms' package
Ken, You just put the submit buttons in the HTML form as you would normally. The submit button isn't part of a Django Model or Form. It really doesn't make sense there; there's no data there. Shawn On Dec 5, 2009, at 9:55 PM, Kenwrote: > I have the following form, which displays nicely: > > class BuySellForm(forms.Form): >symbol = forms.CharField() >shares = forms.CharField() >price = forms.CharField() > > However, I can't figure out how to get multiple 'submit' buttons to > display (one 'buy' and the other 'sell'). Actually, I can't even > figure out how to get a single standard submit button to show up-- > there doesn't seem to be a 'submit widget' listed in the Django book. > Or perhaps I'm just not seeing it? > > As you can tell, I'm an absolute newbie. First day. Thanks for the > patience. > > Ken > > -- > > 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+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-us...@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: How to validate a form field whose value can only increase?
The easiest way I know of is this: In the __init__ of the model, create a variable: self.old_value = self.fieldname. In the save() function, you can check self.fieldname against self.old_value. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to validate a form field whose value can only increase?
On Dec 6, 2009, at 11:01 PM, Continuation wrote: > What do you mean by the __init__ of the model? > Every models.Model object has an __init__ function, like any Python class. You need to override it. Add this into your model (in your models.py): def __init__(self, *args, **kwargs): #make sure you call the default __init__, or stuff breaks. super(YourModelName, self).__init__(*args, **kwargs) #so we can tell if the new value for the field #is increased upon save self.old_value = self.field_name Then, you override the save function for the same model: def save(self): if self.old_value < self.fieldname: raise ValueError('The field x can only increase.') #go through with the standard 'save' stuff #that is defined in models.Model class. super(YourModelName, self).save() This way, your model will not allow itself to be saved if that value is lowered. This doesn't directly answer your question, which was about form validation. I don't know how to do it elegantly, so I hope someone else will chime in. However, one thing you can do is to override the "clean" function of your form for that particular field so that it pulls back an instance of the old object and checks its own cleaned_data value against the value stored in the database. Something like this: #within your forms.ModelForm for the model in question def clean_yourfieldname(self): yourfieldname = self.cleaned_data.get('yourfieldname', 0) old_instance = YourModel.objects.get(pk = self.instance.pk) if yourfieldname < old_instance.yourfieldname: raise forms.ValidationError("The amount in field yourfieldname may only increase.") return yourfieldname To be fair, if you use this in your form validation, you don't really need the stuff above in your model. However, if that is your business logic, then it's a good idea to have it there in any case. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: browser input and strings
What exactly are you trying to do? Can you give a little of the data in your model, and then exactly how you want it to look on the rendered page? I'm having some trouble understanding what your template and view are intended to do. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: model design problem
What kind of information will the database have to store about the ports themselves? How will you need to be able to filter querysets by information in the port fields, if at all? I'm going to throw this one solution out there, but depending on your needs it may not fit: Create a red_ports column and a green_ports column in your model. Each one will contain serialized JSON. You can just dump a Python dictionary into those fields with simplejson.dumps, and retrieve the data with simplejson.loads. Then, you add methods to your model which you can use in the views that deal with the model. Here are a couple of lines of code from one of my models. I'm storing the hashes of old passwords to prevent people from re-using passwords. #field definition in model prev_passwords = models.TextField(default = '{}') I then get and set the values like this: def _get_prev_passwords(self): return simplejson.loads(self.prev_passwords) def _set_prev_passwords(self, pw_dict): self.prev_passwords = simplejson.dumps(pw_dict) previous_passwords = property(_get_prev_passwords, _set_prev_passwords) This way, my model's .previous_passwords attribute is available in my views, and the serialization is transparent. So, your Python dictionaries can contain all kinds of data about the red and green ports, and contain entries for any number of ports. Your models remain clean. The biggest flaw in this method is that, if you need to select a set of products based on an attribute of a port, you'll have to jump through hoops to make it happen. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: insert into data field of model based on other data field values
Easy to do: Just override the save() function of the model. class MyModel(models.Model): def save(self, **kwargs): #add your logic here super(MyModel, self).save(**kwargs) -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to validate a form field whose value can only increase?
On Dec 7, 2009, at 7:16 PM, Continuation wrote: > > I thought if form.is_valid() returns False, Django would send a list > of the errors to browser. But that's not what happened here. Do you have anything like these in your template? {{ my_form.errors }} {{ my_form.field_name.errors }} Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Format fields in list, but keep sortable?
Bear in mind that the admin has a limited scope. This is intentional. It was meant to be a friendly replacement for having to hit the Postgres/mySQL command line interface, and that's about it. It's trivial to create your own form for dealing with your model which can be customized in any way you like. This is not intended to be a snarky response. Partially it's a PSA, and partially a reminder to myself; the admin is so great that it makes me lazy, and I end up living without some really convenient functionality because the admin "almost does it." Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: New to Django, can't get it running on the web through http://127.0.0.1:8000/
We'll need some more information. What error messages are you getting, if anything. How do you know the CMS works at all, if it hasn't been run locally? Have you followed the official tutorial to get a basic understanding of how Django works? If not, please do: http://docs.djangoproject.com/en/dev/intro/tutorial01/#intro-tutorial01 -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: New to Django, can't get it running on the web through http://127.0.0.1:8000/
If you run 'ifconfig' on the command line, you will get your IP address. Let's pretend that your IP address is 192.168.1.105. Try this: python manage.py runserver 192.168.1.105:8000 See if you can visit http://192.168.1.105:8000 instead. I don't know what the problem is, but maybe it's something to do with your hosts file or some firewall issues that's messing with 127.0.0.1. Thanks for providing the command-line output to manage.py, although the fact that it's not giving any errors doesn't help us here. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: New to Django, can't get it running on the web through http://127.0.0.1:8000/
Sorry, I should have said 'ipconfig' instead of 'ifconfig' if you're running on Windows, and you mentioned IE so I guess you are. Unless you're running your Django server on one machine and trying to access it from another, which would definitely cause the exact problem you're seeing, but running it with your actual IP address should fix. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: New to Django, can't get it running on the web through http://127.0.0.1:8000/
I don't think they can prevent you from using it locally. The fact that I can ping the IP address you provided means that it's publicly available. This either means that your machine has its own direct connection to the Internet, or ipconfig returned multiple addresses, including the address of your router, and you chose that one to try. It's probably the latter, because that would definitely cause the error you showed from manage.py runserver. There should be a local IP address for your machine as well. Just try the different ones ipconfig shows until manage.py runsever works with it. I supposed it's possible that a proxy server is in place in your browsers by default, although I'd expect them to ignore 127.0.0.1. But out of curiosity, please check your Firefox settings to see if any proxying is taking place. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: New to Django, can't get it running on the web through http://127.0.0.1:8000/
On Dec 8, 2009, at 11:38 AM, Jenn Hughes wrote: > I checked and no proxy is in place. I tried the other IPs, two > wouldn't let me assign it and the third went through but gave me the > same error that I began with. How odd. > I suspect there may be some other tiny but very important problem with one of your files. Unfortunately, it's probably not something one of us can guess without more information, and you're not getting any informational error messages we can use to narrow it down. I suggest you work through the official tutorial step-by-step. That way, you should find out exactly where the problem lies. If you don't get the tutorial to work, knowing the point of failure should help a lot. If you do get it to work, then you can compare your project with the tutorial and see what's different. You should be able to zip through enough of the tutorial in 10 or 15 minutes to get your answer. http://docs.djangoproject.com/en/dev/intro/tutorial01/#intro-tutorial01 Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to choose one row of data
If you assign the output of the save() method, you'll get the created object. Instead of: form.save() do: current_order = form.save() Then you can do whatever you like with current_order, which will be an instance of whatever the class 'meta' is of your OrderForm, assuming that OrderForm is a forms.ModelForm. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to populate the database
There are different ways to do it, depending on how much data you have and how often you plan to do it. The fastest way for large files is to use sqlite3's .import command to directly import a file. However, this will bypass any validation done by your models. The way I do it is to read the csv with csv.DictReader, then create instances of my model with the values from the resulting dictionary, then calling a .save() on the new instance. This is fairly slow, but thorough; you won't realize belatedly that your database is missing required fields or has invalid values, because the script will just blow up if you try. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: question about changing model/table names and attributes ... (still learnin')
The current best tool for this is South: http://south.aeracode.org/ You can do complicated data and database migrations smoothly, with the ability to roll-back changes. I don't know why you mention changing table names, since that should never be necessary. But anything is possible. If you're doing "major" changes, defined (by me) as creating a new thing (field or table, A.K.A. attribute or model), moving data out of an existing thing into it, then deleting the original thing, you'll want to carefully read and understand South's excellent strategy for doing this; http://south.aeracode.org/wiki/Tutorial3 All your normal stuff, such as adding attributes to models, changing properties (required or not, default values, and so on) are a piece of cake with South. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to populate the database
Sure, here's a quick & dirty sample I put up on pastebin: http://pastebin.com/f651cf8de -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to execute linux command in django view
The proper way to do that is to use the subprocess module. http://docs.python.org/library/subprocess.html If you don't want to do much reading, you'll just want to look at section 18.1.3.1, which gives the way to call a simple shell command the way you'd do with backticks in Perl. However, since you mentioned fdisk, maybe you're actually wanting to interact with the shell command. That's more complicated, but can be done with the "communicate" method of a subprocess. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: HTML text editing
Look at safestring. from django.utils import safestring The docs will explain all, but basically you do this: the_html = safestring.mark_safe(your_html) The text in "the_html," if passed in your template, will not be escaped. The standard warning should be repeated here -- be sure you trust your users, or take measures to prevent the insertion of scripting before you allow anything they enter to be displayed. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Error creating and saving a Django-object from the other script
On Dec 11, 2009, at 4:51 PM, tezro wrote: > Seems like no clues... > > On Dec 10, 12:21 am, bruno desthuilliers >wrote: >> On 9 déc, 18:58, tezro wrote: >> (snip) >> >> >> >>> That throws an error: TypeError: 'slug' is an invalid keyword argument >>> for this function. >> >>> What am I doing wrong? >> >> Not posting the full traceback - tracebacks are here to help debugging >> a problem, not to fill your term with random gibberish !-) > > -- > > 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+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > Tezro, What Bruno meant is that, unless you post *all* of the text that printed out around the error message (not just that one line), you have not provided enough information for us to help you. Anyone on this list can probably tell you exactly what that TypeError means. But telling you that won't solve your problem. This error: >>> TypeError: 'slug' is an invalid keyword argument means that somewhere in your Python code, the keyword argument slug = "some value" is being passed to a function. That function doesn't know what to do with a 'slug' keyword, because that function's definition doesn't accept it, nor does it have a **kwargs portion. So, although I've told you what the problem is, I haven't helped you much with your question "what am I doing wrong." But if you provide more of the information about the error, and preferably the section of code that is generating it, you will get help that is much more useful to solving the problem. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Error creating and saving a Django-object from the other script
Why did you manually add a 'slug' field to your database after modifying the model? Django's syncdb does a lot more than just create the field; depending on which database you're using, it does a few other things and overrides some defaults. I wonder if there's a disconnect there. If this project is currently in production and you can't re-run syncdb, then I highly recommend South (south.aeracode.org) for database migrations. I suspect that the problem is because the field was added manually. Try adding the field with South. It takes care of the nitty gritty details for you. Worst-case, make a copy of the model in question with a different name. Then try to create new instances of it from your external script. If that works (after adding that new model to the database with syncdb), then it's almost definitely due to the way the model and database were modified after-the-fact manually. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Passing data from view to template
What kind of object are you trying to send? JSON pretty much looks identical to a Python dictionary, so if your object is a Python dictionary, list, or tuple you'll have no problem -- just convert the Python object using simplejson.dumps(). If this doesn't help much, please provide more detail about the object you want to send and what you want to do with it in JavaScript. In general, though, you'll need to serialize the object somehow so that JavaScript can make use of it. I think simplejson will end up being part of your solution, regardless of what you do. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Can I change the models.py of an existing django app?
I feel like a broken record this week -- I've been recommending South in every other post I've made on this list. So, at risk of annoying people, check out South: http://south.aeracode.org It does exactly what you want it to do, and is the dominant (and nearly the only) solution used in the Django community. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Passing data from view to template
You don't need to transfer this to JavaScript once it's in JSON, which is a JavaScript object. If you're using AJAX, it's dead easy. #use this in your view return HttpResponse(simplejson.dumps(employees), mimetype="application/json") Then, in your JavaScript, you can use JSON syntax to refer to the items and attributes in your object. Here are a couple of examples I posted recently, showing a GET and POST example: http://groups.google.com/group/django-users/browse_thread/thread/45ba4d8f0f71d8d6/ed1002002178633d?hl=en=gst=shawn+json+balance#ed1002002178633d Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Error creating and saving a Django-object from the other script
On Dec 12, 2009, at 8:08 AM, tezro wrote: > Nope. Did it again on a clean project with the same models migrated > then by South. > > Same error. Any other clues? > > Thanks for replies. When you say "same error," do you mean this one: "Duplicate key name 'news_element_slug'"? If so, then it appears that, despite the clean project, it's not a clean database. Try using sqlite3 temporarily, or change the model name from News to NewsTest or something. Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: The best way to populate sqlite3 database
The fastest way for large files is to use sqlite3's .import command to directly import a file. However, this will bypass any validation done by your models. You could end up with "bad" data, in that it doesn't conform to the rules of your models. Or, you can just write an external script to read the file and create model instances. Here's a brief example: http://pastebin.com/f651cf8de Shawn -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Testing: Fixtures vs Factories vs ???
I don't think there's one true way. In other words, the answer is "all of the above," depending on your project and the needs of each individual test. It also depends on your code. If you've done TDD, and therefore made your code easier to test, you can probably do it the simplest way possible. If you're trying to add coverage after the code is in place, there's often a large amount of "setup" needed due to model relationships, and it's easier to just dump everything into fixtures and write tests to your known data. There's also a third option, which is to have your tests (or the setUp) dynamically create data for use by the tests. This is really handy for things that are easy to test, but can be too much depending on the design of the code overall. Shawn -- 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: Script to call loaddata on multiple fixtures
Why not just put all those manage.py commands in a shell script and run that? -- 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: Related Objects
I think what you're looking for is annotate(): https://docs.djangoproject.com/en/1.3/topics/db/aggregation/ -- 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: Associating Form Data with Users
When you process the form in your view, you'll have access to request.user. Just use that. -- 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: Associating Form Data with Users
On 02/19/2012 09:29 PM, ds39 wrote: Thanks for your response. But, would you mind expanding on it a little bit ? How about you give it a try and see what you can figure out? In your view, request.user will return the currently logged-in user (or an AnonymousUser if they're not logged in). Since you said your view requires login, you'll have a User object all ready to go. Shawn -- 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: I am getting TypeError: coercing to Unicode: need string or buffer, long found. Please help.
Read the error message in your subject line. Then look at the __unicode__ method of your Phone model. It appears that this is the problem, and not the Device model. -- 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: About managing dependencies in a collaborative development team and good practices.
On 02/21/2012 10:53 AM, Javier Guerra Giraldez wrote: i do exactly that. just a tip: to create and maintain the pip requirements file do: pip freeze> piprequirementsfile.txt +1 And it's checked into version control. Shawn -- 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: I am getting TypeError: coercing to Unicode: need string or buffer, long found. Please help.
On Tue, Feb 21, 2012 at 12:18 PM, Daniel Marquez < daniel.marquez0...@gmail.com> wrote: > Wow, I should've caught that. Thanks guys. However, since I needed a > string, what I did was add "default=x" to the integer field as > follows: > > class Phone(models.Model): >phonenumber = models.IntegerField(default=10) >pub_date = models.DateTimeField('date published') >def __unicode__(self): >return self.phonenumber > > and it worked. > > As mentioned earlier, your phone number field should not be an integer. Among other reasons, using a string allows you to validate for different countries, have extensions, and more easily manipulate and display the value everywhere you use it (you're never going to use it *as* an integer, after all). Also, setting an integer field to have a default value other than zero smells really bad. Shawn -- 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: psycopg2, postgres9.1, Mac OS - problem with psycopg2
You can either add the proper path of pg_config to your PATH, or just extract the psycopg2 and add the full path to pg_config into the config file it contains then run 'python setup.py install' on the setup.py in the package. -- 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.