I'm trying to save an object in a non-default database. I have already configured a second connection.
I know that in order to get objects it is Class.objects.using('the_db').all
But when I try object.using('the_db').save() it doesn't work. (Error: "object has no attribute 'using'".
I also tried object.save('the_db') but it also does not work.
How can I achieve this? I couldn't find the correct syntax.
Try object.save(using='the_db')
Relative django documentation: https://docs.djangoproject.com/en/2.2/topics/db/multi-db/#moving-an-object-from-one-database-to-another
Related
I have a problem with my Django project. I need to delete 1 or more users from my database but I can't do it because it returns this error:
__str__ returned non-string (type tuple)
I tried deleting it from my views but it didn't work. Than I went and used admin panel to try to delete it but the same problem happened.
If someone could give some advice or suggestion that would be great.
Thank you.
I found out what was the problem. I was returning tuple in my models __str__ function instead of a string which worked while users existed but it for some reason didn't when I tried to delete or as I later found out create a new user.
Django supports using multiple databases simultaneously, and the docs explain how to specify the database when saving or retrieving an object. However, given a Django model instance, how do you determine which database it's currently using? I've inspected an instance, but it doesn't seem to have any .using attribute or anything similar.
From the Using routers documentation, you can use model_instance._state.db to inspect the database which the current model instance is using.
In Django, how does one give an attribute field name an alias that can be used to manipulate a queryset?
Background: I have a queryset where the underlying model has an auto-generating time field called "submitted_on". I want to use an alias for this time field (i.e. "date"). Why? Because I will concatenate this queryset with another one (with the same underlying model), and then order_by('-date'). Needless to say, this latter qset already has a 'date' attribute (attached via annotate()).
How do I make a 'date' alias for the former queryset? Currently, I'm doing something I feel is an inefficient hack: qset1 = qset1.annotate(date=Max('submitted_on'))
I'm using Django 1.5 and Python 2.7.
Even if you could do this, it wouldn't help solve your ultimate problem. You can't use order_by on concatenated querysets from different models; that can't possibly work, since it is a request for the database to do an ORDER BY on the query.
It seems qset1 = qset1.annotate(date=Max('submitted_on')) is the closest I have right now. This, or using exclude(). I'll update if I get a better solution. Of course other experts from SO are welcome to chime in with their own answers.
I'd like to save a WSGIRequest to a Django model to do some debugging. Is it possible to create a model to do this? I get all kinds of errors when I try:
class MyRequest(models.Model):
request = WSGIRequest()
Thanks in advance.
Why would you expect to be able to persist an object with all kinds of ephermal state to the database?
You best bet is probably to write a function that takes a Request object, pulls out whatever information is relevant to you and creates a string with the info in persist that.
Doing a json.dumps on the whole object work.
I'm using formish to handle web forms but I don't like the automatically generated error messages when validation fails. Where do I customize those error messages?
The best place for feedback on formish is in the google groups which is linked to from the http://form.ish.io page..
As for customising the error messages, the best way would be to create your own validator (which is pretty simple, have a look in the validatish module). However, you've inspired me to think about making all of the validators take a custom argument for each type of message... I'll have a think about that one. If you struggle customising your own errors, drop into the google group...
I've added custom messages to the validatish module for you .. If you get it from github, you'll see how it works (you pass a dictionary of messages to the validate function or object - e.g. Required(messages={'required': 'dont forget it!'}) )
If you need a release for it quickly, let me know..