Is there any nothing wrong with this raw query?
Worker.objects.raw('Delete from customer_worker Where customer_ptr_id= %s', [customer.id])
Customer id returns a string, but it seems like nothing happen, the object is still there after the execution.
The object Worker is a child object from Customer, I want to remain the customer, but delete the Worker object.
Here are the Customer and Worker models:
class Customer(User):
slug=models.SlugField(unique=True)
description=models.TextField(null=True)
phone=models.IntegerField(null=True)
isWorker=models.BooleanField()
def save(self,*args,**kwargs):
self.slug=slugify(self.username)
super(Customer,self).save(*args, **kwargs)
def __unicode__(self):
return self.username
class Worker(Customer):
comment=models.ForeignKey(Comment, null=True)
keyword=models.ManyToManyField('job.JobGenre', null=True)
def __unicode__(self):
return self.username
You can delete your record(s) directly via connection.cursor() (docs):
from django.db import connection
cursor = connection.cursor()
cursor.execute('DELETE FROM costumer_worker WHERE costumer_ptr_id = %s', [costumer.id])
connection.commit()
But, the thing you're trying to do looks too simple to write SQL directly, use django ORM instead.
You may be missing comitting the transcation. Based on your settings, you may need to add :
transaction.commit_unless_managed() in django 1.4 ( not needed in django 1.5, since default settings are different)
use this line:
from django.db import connection, transaction
cursor = connection.cursor()
with transaction.commit_on_success():
cursor.execute('DELETE FROM costumer_worker WHERE costumer_ptr_id = %s', [costumer.id])
connection.commit()
Related
I am trying to filter on a foreign key but getting an error.
Current code is:
views.py
def kingmailboxcodesshow(request):
lname = "King"
lockbox_list = MailBoxCodes.objects.raw('SELECT * FROM mailboxcodes WHERE Address_id__contains %s',[lname])
return render(request,"users/mailboxcodesshow.html",{'MailBoxCodes':lockbox_list})
models.py
from django.db import models
from properties.models import Properties, Building_Name
from django.db.models import Q
# Create your models here.
class MailBoxCodes(models.Model):
id = models.AutoField(primary_key=True)
Address = models.ForeignKey(Properties, max_length=10, on_delete=models.CASCADE, default='Unknown',limit_choices_to=Q(StudentRental=True)| Q(Active=True))
MailBoxCode = models.CharField(max_length=10, null=False,default='000')
Active = models.BooleanField(default=True)
class Meta:
db_table = "mailboxcodes"
def __str__(self):
return str(self.Address)
receiving this error:
django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''King'' at line 1")
I am still really new to django and python, looking at the error I am thinking i need a few less ' around the King, but I am not sure how to make that happen.
I have a bunch of addresses in the Address_id and I just want to retrieve all the address with the work King in their street address.
The great thing with Django is that you don't need raw SQL queries, unless you really really know what you're doing. Please follow the official tutorial as it explains the fundamentals of Django and will guide you through this over here: Django tutorial
To help you out with this particular issue:
def kingmailboxcodesshow(request):
lname = "King"
lockbox_list = MailBoxCodes.objects.filter(address__name__contains=lname)
return render(request,"users/mailboxcodesshow.html",{'MailBoxCodes':lockbox_list})
Well there is no such thing as a __contains in SQL, that is just some Django logic that transforms it to a query, you can work with:
def kingmailboxcodesshow(request):
lname = 'King'
lockbox_list = MailBoxCodes.objects.raw(
'SELECT * FROM mailboxcodes WHERE Address_id LIKE %%%s%%', (lname,)
)
return render(
request, 'users/mailboxcodesshow.html', {'MailBoxCodes': lockbox_list}
)
That being said, using raw queries should be a last resort: the entire idea of Django's ORM is, as you actually found out with this question to make abstraction of query logic. You can run this with:
def kingmailboxcodesshow(request):
lname = 'King'
lockbox_list = MailBoxCodes.objects.filter(Address_id__contains=lname)
return render(
request, 'users/mailboxcodesshow.html', {'MailBoxCodes': lockbox_list}
)
Please share your MailBoxCodes model, and also tell what is this Address_id as you mentioned it is a foreign key in one of the comments above.
Try this:
def kingmailboxcodesshow(request):
lname = 'King'
lockbox_list = MailBoxCodes.objects.filter(Address__name__contains=lname)
return render(
request, 'users/mailboxcodesshow.html', {'MailBoxCodes': lockbox_list}
)
Or this:
MailBoxCodes.objects.filter(Address__contains=lname)
You can also use __icontains lookup for case-insensitive filtering.
I working in a project in which i have different projects with the same database architecture,
so i used peewee Model in which:
dynamic_db = SqliteDatabase(None)
class BaseModel(Model):
class Meta:
database = dynamic_db
class KV (BaseModel):
key = TextField()
value = IntegerField()
And whenever i new project is created i will call a function
dynamic_db.init(r'{}\database.db'.format(ProjectName.upper()))
dynamic_db.connect()
dynamic_db.create_tables([KV])
dynamic_db.close()
The problem is that once this database is created, i can't access with peewee.
When i try to create a record:
KV.create(key = 'Saul', value = 123)
I get this error:
peewee.InterfaceError: Error, database must be initialized before opening a connection.
I would appreciate any help or cookbook for peewee.
I believe something is incorrect, either in your question description, or in the error you are receiving. The call you are making to .init() is what initializes the database. After that, you should have no problems using it.
Full example which works fine:
from peewee import *
db = SqliteDatabase(None)
class Base(Model):
class Meta:
database = db
class KV(Base):
key = TextField()
value = IntegerField()
db.init('foo.db') # database is now initialized
db.connect()
db.create_tables([KV]) # no problems.
db.close()
I was finally able to create a record.
I didn't mention that i was trying to create them in another file, but the procedure is the same as the one coleifer posted on the answer.
The file in which i create the peewee models is databases.py, so in the other file i do the following:
import databases
databases.db.init('foo.db')
databases.KV.create(name = 'Saul', value= 123)
Thanks!
i have created 2 table, ie table1 and table2 i want to insert data in to both tables using django ORM , how can i achieve it
models.py
class Table1(models.Model):
name = models.CharField(max_length=20,null=True)
class Table2(models.Model):
name = models.CharField(max_length=20,null=True)
views.py
class Test(ListAPIView):
def get(self,request):
obj1 = Table1(name="jasir")
obj2 = Table2(name="shibin")
obj1.save()
obj2.save()
return Response(True)
im saving like this but i want to save it using single save() instance is there any possiblity
the equivalent sql query i found is
BEGIN TRANSACTION
INSERT INTO Table1 (name) VALUES ('jasir')
INSERT INTO Table2 (name) VALUES ('shibin')
COMMIT TRANSACTION
how to do the same with django ORM
Try making the saves atomic like this:
with django.db.transaction.atomic():
obj1.save()
obj2.save()
you can use Django's transcation.atomic context-manager to do that
Refer to:
https://docs.djangoproject.com/en/2.2/topics/db/transactions/#django.db.transaction.atomic
with transaction.atomic():
# This code executes inside a transaction.
obj1 = Table1(name="jasir")
obj2 = Table2(name="shibin")
obj1.save()
obj2.save()
For example assume that I have 100 clients who uses WordPress and I have to write a service in Django which should return list of posts from WordPress's MySQL DB. The problem is 100 clients are having different database connection settings.
I know that I can use DatabaseRouter to switch databases which are already loaded in settings. But I don't know how to make a singe model class to use different database settings.
I have tried mutating settings.
I also tried mutating model's app_label.
But I later understood that mutating anyting in Django is meaning less.
My Requirements
I want to create a model and dynamically change database connection. List of connection can be in a managed database table. But I don't want to unnecessarily load all the connection settings or create multiple models.
I made something like that, but to change mongodb connections.
I created a GenericView that select the connection and use it on the get_queryset.
I'm using django rest framework, so I made something like this:
class SwitchDBMixinView(object):
model = None
fields = None
def initial(self, request, *args, **kwargs):
result = super().initial(request, *args, **kwargs)
if request.user.is_authenticated():
request.user.database_connection.register()
return result
def get_object(self, *args, **kwargs):
return super().get_object(*args, **kwargs).switch_db(self.get_db_alias())
def get_db_alias(self):
if self.request is None or not self.request.user.is_authenticated():
return DEFAULT_CONNECTION_NAME
return self.request.user.database_connection.name
def get_queryset(self):
return self.model.objects.using(self.get_db_alias()).all()
def perform_destroy(self, instance):
instance.switch_db(self.get_db_alias()).delete()
The model:
from mongoengine.connection import register_connection, get_connection
AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL')
class Connection(models.Model):
class Meta:
pass
owner = models.OneToOneField(
AUTH_USER_MODEL,
related_name='database_connection',
)
uri = models.TextField(
default=DefaultMongoURI()
)
def register(self):
register_connection(
self.name,
host=self.uri,
tz_aware=True,
)
get_connection(
self.name,
reconnect=True
)
def get_name(self):
return 'client-%d' % self.owner.pk
name = property(get_name)
def __str__(self):
return self.uri
You may want to have a look at django.db.connections (in django/db/__init__.py) and django.db.utils.ConnectionHandler (which django.db.connections is an instance of). This should let you dynamically add new db configs without hacking settings.DATABASES (actually ConnectionHandler builds it's _databases attribute from settings.DATABASES). I can't tell for sure since I never tried but it should mostly boils down to
from django import db
def add_db(alias, connection_infos):
databases = db.connections.databases
if alias in databases:
either_raise_or_log_and_ignore(your choice)
db.connections.databases[alias] = connection_infos
where connection_infos is a mapping similar to the ones expected in settings.DATABASES.
Then it's mostly a matter of using Queryset.using(alias) for your queries, ie:
alias = get_alias_for_user(request.user)
posts = Post.objects.using(alias).all()
cf https://docs.djangoproject.com/en/1.11/topics/db/multi-db/#manually-selecting-a-database
The main problem with this IMHO (assuming you manage to make something that works out of the untested suggestion above) is that you will have to store databases users/password in clear somewhere which can be a major security issue. I don't know how much control you have on the databases admin part but it would be better if you could add a 'django' user with a same password (and appropriate permissions of course) on all those databases so you can keep the password in your settings file instead of having to keep it in your main db.
I have a table in a database which is created and accessed through SQLAlchemy:
I add a record to it using Flask-SQLAlchemy like so:
...
content = request.form['content']
date = datetime.today()
post = Post(date, content)
db.session.add(post)
db.session.commit()
...
This record is added to the table fine. Right after that code is executed, I query another table:
userID = session['userID']
posts = db.session.query(Post).filter_by(userID=userID).count()
However I receive an error during the query:
OperationalError: (raised as a result of Query-invoked autoflush;
consider using a session.no_autoflush block if this flush is occurring
prematurely) (_mysql_exceptions.OperationalError) (1292, "Incorrect
date value: '11/20' for column 'date' at row 1") [SQL: u'UPDATE
posts SET date=%s WHERE posts.id = %s'] [parameters: (('11/20',
1L))]
Why is the date of the post being updated when I have already specified it when adding the record to the table? Also what could the cause of this error be? Thanks.
Edit:
This is what the table model is like:
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(500))
date = db.Column(db.Date, nullable=False)
def __init__(self, id, content, date):
self.id = id
self.content = content
self.date = date
Stephane is right, you are passing a wrong type to the model, either pass datetime.date object or change the definition of the model. As to the first part of the question, I recommend reading something about sessions and flushing. This is important:
All changes to objects maintained by a Session are tracked - before the database is queried again or before the current transaction is committed, it flushes all pending changes to the database.
So by creating the post object and adding it to the session, you made just a pending change, but there was no communication with the database at that point yet. That happens with flush(), which you can either call manually or automatically, for example, by calling commit().
(btw. you dont need to create your own init method for the model, see http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#adding-and-updating-objects)
date = datetime.today() returns a datetime object (date AND time)
but the date attribute of the Post model is a db.Date (date WITHOUT time)
Try either :
from datetime import date
...
content = request.form['content']
date = date.today() #inject a Date object rather than a Datetime
or:
class Post(db.Model): #modify Post schema
...
date = db.Column(db.TIMESTAMP, nullable=False)
This error can cause from another query,
even if you solve it that exceptions will still occured if you not rollback previous session error
You can catch exception and rollback transaction
usually in my flask application, I commit session in end of request
#app_instance.after_request
def after(response):
try:
# commit transaction
db.session.commit()
except Exception:
db.session.rollback()
raise
return response