django model building table name wrong - python

I have a postgresql RDS instance on aws free tier with my ec2 instance. I should have used EB, but didn't really know about it until I had a lot built out and am not excited about starting over on a new ami. I am trying to build my first interaction with the db, have a table there called server_config, created as:
CREATE TABLE SERVER_CONFIGS
(
config_key VARCHAR(63) NOT NULL,
config_value VARCHAR(63) NOT NULL,
UNIQUE (config_key)
);
INSERT INTO SERVER_CONFIGS (config_key,config_value)
VALUES ('ClientMustVerify','TRUE');
INSERT INTO SERVER_CONFIGS (config_key,config_value)
VALUES ('Auditing','FALSE');
COMMIT;
and in my django app, I have:
models.py:
from django.db import models
class serverConfig(models.Model):
config_key = models.CharField(max_length=63)
config_value = models.CharField(max_length=63)
excerpt of view.py:
from limbo.models import serverConfig
def editServer(request):
myConfigs = serverConfig.objects.all()
configHtml = ""
# for item in myConfigs #serverConfig.objects.values()
# configHtml += item.config_key + "\t" + item.config_value + "\n"
if request.method == 'POST':
form = serverForm(request.POST)
if form.is_valid():
integer = form.cleaned_data['int_field']
request.session['integer'] = integer
# call out to limboLogic.py to update values, add them to the session
message = 'The value \'' + str(integer) + '\' has been updated.'
return render(request, 'limboHtml/ServerConfiguration.html', {'form': form, 'SubmitMessage': message, 'CurrentConfigs': myConfigs})
else:
message = 'The server configuration has NOT been updated.' + '\n'
message += ', '.join("%s=%r" % (key,val) for (key,val) in form.errors.iteritems()) + '\n'
# message += ', '.join("%s=%r" % (key,val) for (key,val) in form.non_field_errors.iteritems()) + '\n'
return render(request, 'limboHtml/ServerConfiguration.html', {'form': form, 'SubmitMessage': message, 'CurrentConfigs': myConfigs})
# if a GET (or any other method) we'll create a blank form
try:
del request.session['integer']
except KeyError:
pass
form = serverForm()
return render(request, 'limboHtml/ServerConfiguration.html', {'form': form, 'SubmitMessage': '', 'CurrentConfigs': myConfigs})
error message:
File
"/home/ec2-user/limbo/limboenv/local/lib/python2.7/site-packages/django/db/backends/utils.py",
line 64, in execute
return self.cursor.execute(sql, params) ProgrammingError: relation "limbo_serverconfig" does not exist LINE 1: ...ig_key",
"limbo_serverconfig"."config_value" FROM "limbo_ser...
Why is it appending limbo_ at the start of the table? any way I can change this? should I just use my app name (or is that my project name? they're called the same thing...stupid polls tutorial) in the tables?
Django 1.10 and python 2.7 on a linux ami on ec2

Django creates a table for each of your models. The default name of the table will be app_name + '_' + model_name. If you do not want that to be the table's name, you can override it by specifying db_table in the models's Meta class
Ref: https://docs.djangoproject.com/en/1.10/ref/models/options/#db-table
Suggestion
Please do not use the app with the same name as the project to write your application logic. That app should always contain only the project settings

Related

Django queryset not returning all rows

Hi I have below setup.
Django 1.9,
mongoDB,
pymongo 2.7,
mongoengine 0.9
I have written an API to store logs at backend server userwise. Below is sample of table
user subject registered changed_on
abc eng Y "2018-04-18T00:00:00Z"
abc maths N "2018-04-18T00:10:00Z"
xyz eng Y "2018-04-18T00:10:00Z"
I also have read API for this in which we give user name and timestamp for filter like below:
{
"user" : "abc",
"from_date" : "2018-04-18T00:00:00Z"
}
The line in serializers.py which is applying filter is
Logs.objects.filter(user__iexact='abc',changed_on__gte=from_date)
Now Sometimes when I add new log and retrieve it from postman, it is not working. I have to restart django server and then it gives me newly added row.
I dont understand why this is happening.
EDIT1 : Full Serializer.py
from rest_framework import serializers
class GetUserLogs(serializers.Serializer):
user = serializers.CharField(label=_("USER"))
token = serializers.CharField(label=_("Token"))
from_date = serializers.CharField(label=_('From date'), default="")
till_date = serializers.CharField(label=_('Till date'), default=datetime.datetime.now().isoformat().split(".")[0]+'Z')
def validate(self, attrs):
user = attrs.get('user')
token = attrs.get('token')
from_date = attrs.get('from_date')
if user:
tokendetails = validate_token(token)
if not tokendetails:
msg = _('Invalid token.')
raise serializers.ValidationError(msg)
else:
userdetails = tokendetails.user
if userdetails.check_user(user):
rows = Logs.objects.all().filter(user__iexact=user,changed_on__gte=from_date, changed_on__lte = till_date)
print(len(rows))
else:
msg = _('Invalid USER)
raise serializers.ValidationError(msg)
else:
msg = _('Must include "USER".')
raise serializers.ValidationError(msg)
attrs['rows'] = rows
return attrs
So After a lot of debugging for almost a day, I found that the default value of till_date that we are setting to current timestamp of system is not increasing as time increase. So I changed default value to empty string and added below code after reading value of till_date.
if till_date == "":
till_date = datetime.datetime.now().isoformat().split(".")[0]+'Z'
It will reset the till_date value to current timestamp every time its value is missing.
Hope it will help someone someday.

How can I use Geoalchemy2 to update spatial data (like point)?

I just write a small website to show some spatial data using flask and Geoalchemy2. Now I can insert some new spatial records (for example, points) into my postgresql database but having some troubles when I want to update them.
my code is as below.
Model.py:
class Geopoint(db.Model):
"""point class"""
__tablename__ = 'geo_point'
ptid = db.Column(db.Integer, primary_key=True)
quiztime = db.Column(db.Numeric)
geopt = db.Column(Geography(geometry_type='POINT', srid=4326))
init.py:
db = SQLAlchemy()
geo_engine = create_engine('postgresql://postgres:password#localhost/database', echo=True)
view.py:
geo_session_class = sessionmaker(bind=geo_engine)
geo_session = geo_session_class()
if request.method == 'POST':
if geo_type == 'POINT':
pt_res = geo_session.query(
Geopoint.ptid,
Geopoint.quiztime,
Geopoint.geopt.ST_AsText()
).filter_by(ptid=geo_id).first()
if pt_res:
print pt_res
else:
geo_session.add(
Geopoint(
quiztime=time.time(),
geopt=geo_type + '(' + geo_coord.encode('utf-8') + ')'
)
)
geo_session.commit()
My code works when I add a new point record.
When I edit the existed point my code of update part returns the printed result (I just want to know how to write it.):
(4L, Decimal('1508430387.581'), u'POINT(120.057, 30.262)')
that does not look like a class but a tuple so I can not update it with
Geopoint.geopt=.....
db.session.add(Geopoint)
db.session.commit()
In the official document there are only examples to add new objects into the database so I am really confused.
Is there any MAGIC sentence to update the data or is there any other geography orm libraries to use?
Appreciation for any answer.
emmmmmm……
Finally I figure out it myself.
Actually the change is very simple. I just change the query object so it return a Geopoint object to me that can be updated.
geo_session_class = sessionmaker(bind=geo_engine)
geo_session = geo_session_class()
if request.method == 'POST':
if geo_type == 'POINT':
pt_res = geo_session.query(
Geopoint.ptid,
Geopoint.quiztime,
Geopoint.geopt.ST_AsText()
).filter_by(ptid=geo_id).first()
if pt_res:
print pt_res
else:
geo_session.add(
Geopoint(
quiztime=time.time(),
geopt=geo_type + '(' + geo_coord.encode('utf-8') + ')'
)
)
geo_session.commit()
change to this.
if request.method == 'POST':
if geo_type == 'POINT':
pt_res = geo_session.query(Geopoint).filter_by(ptid=geo_id).first()
if pt_res:
print pt_res
else:
geo_session.add(
Geopoint(
quiztime=time.time(),
geopt=geo_type + '(' + geo_coord.encode('utf-8') + ')'
)
)
geo_session.commit()
Then my update code is possible to write.
=。=

Redis TypeError: must be string or buffer, not None

I want to set a django model in my redis data store, and then in another view function, I want to get it and reuse it again; but it turns out to say:
TypeError: must be string or buffer, not None.
This is my code in views.py:
connection = redis.Redis('localhost')
def recharge_account(request):
cur = recharge_form.cleaned_data['currency']
amnt = recharge_form.cleaned_data['amount']
user_profile = models.UserProfile.objects.get(user=models.User.objects.get(id=request.user.id))
user_b_account, created = models.BankAccount.objects.get_or_create(
owner=user_profile,
cur_code=cur,
method=models.BankAccount.DEBIT,
name=request.user.username + '_' + cur + '_InterPay-account',
account_id=make_id()
)
# saving the temporarily deposit in redis
deposit_dict = {"account": user_b_account, "amount": amnt, "banker": user_profile,
"date": user_b_account.when_opened, "cur_code": cur}
pickled_deposit_dict = pickle.dumps(deposit_dict)
cached_deposit_name = str(user_profile.user_id) + "-cachedDeposit"
connection.set(cached_deposit_name, pickled_deposit_dict)
....
def callback_handler(request, amount):
#getting from redis
new_deposit = pickle.loads(
connection.get(str(user_profile.user_id) + "-cachedDeposit"))
deposit = models.Deposit(account=new_deposit['account'], amount=new_deposit['amount'],
banker=new_deposit['banker'],
date=new_deposit['date'], cur_code=new_deposit['cur_code'])
deposit.save()
.....
My question is: is there any problem with my procedure? Is there any problem with saving a django model in redis?
What should I do to get this function to work properly and getting the saved data "not to be None"?

Creating new table while iterating through a queryset in django

This is a newbie question, but despite reading https://docs.djangoproject.com/en/dev/ref/models/instances/#saving-objects , I'm not quite sure how to do this. I have an existing table where I would like to iterate through all its records, and save certain info to a second table. I have the following model:
class myEmails(models.Model):
text = models.CharField(max_length=1200)
In my view I have:
def getMyMessages(request):
from django_mailbox.models import Message
from get_new_emails.models import myEmails
import re
qs = Message.objects.all()
count = 0
output = ""
for i in qs:
count += 1
output = output + str(count) + " TEXT: " + i.text + '<br>' + '<br>'
return HttpResponse(output)
How can I modify my view to save "i.text" to the text field of the 'myEmails' table
You can create new objects and save them to the database afterwards using save():
for i in qs:
obj = myEmails(text=i.text)
obj.save()

Modified django form cleand_data do is not renedered in data

I want to add data to submitted data in a django form.
Until now I did something like this:
form = NewADServiceAccount(data=request.POST)
if form.is_valid():
data=request.POST.copy()
if not 'SVC' in data['Account_Name']:
data['Account_Name'] = 'SVC_'+data['Account_Name']
form = NewADServiceAccount(data=data)
This works, but I would like to do this check in a clean method, so I defined:
def clean_Account_Name(self):
data = self.cleaned_data['Account_Name']
if not 'SVC' in self.cleaned_data['Account_Name']:
data = 'SVC' + data
return data
However, when I run this code with the clean method, I see that clean_data does not match data,
and my rendered form does not contain a correct Account_Name (e.g. it does not have SVC in it):
ipdb> p form.cleaned_data['Account_Name']
u'SVCoz123'
ipdb> p form.data['Account_Name']
u'oz123'
The Account_Name from data is the one rendered to HTML, how can I fix this, so that Account_Name from cleaned_data is rendered?
update:
I found another way to do it, but I am still not sure it is the right way:
# inside forms.py
class NewADServiceAccount(NewAccount):
Account_Name = forms.CharField(min_length=3, max_length=21, required=True,
#initial="Please write desired name of "
#+ "this service account.",
help_text="Please write the desired name "
+ "of this account. The prefix 'SVC_' will"
+ " be added to this name",)
def set_prefix(self, prefix='SVC_'):
data = self.data.copy()
data['Account_Name'] = prefix+data['Account_Name']
self.data = data
# in views.py:
if form.is_valid():
form.set_prefix()
second update:
After looking at my solution I decided my clean method can be a bit better, so I did:
def clean_Account_Name(self):
data = self.data.copy()
if not 'SVC' in data['Account_Name']:
data['Account_Name'] = 'SVC' + data['Account_Name']
self.data = data
the above solution works, although the django documentation says:
Always return the cleaned data, whether you have changed it or not.
So, now I am quite confused.
I found a solution, but I need reaffirming it is a valid and good one. I would be happy if someone comments here about it.
If I understood you have been attempts uses the clean method. If I right, you did it a little wrong. Try use def clean() with a form's field:
forms.py
class AccountNameField(forms.CharField):
def clean(self, value):
value = u'SVC' + value
return value
class NewADServiceAccount(forms.Form):
Account_Name = AccountNameField(min_length=3, max_length=21, required=True,
#initial="Please write desired name of "
#+ "this service account.",
help_text="Please write the desired name "
+ "of this account. The prefix 'SVC_' will"
+ " be added to this name",)
views.py
form = NewADServiceAccount(request.POST or None)
if form.is_valid():
...
prefix is used only into a forms. If I am not mistaken prefix would be assign each fields of the form as prefix-namefield

Categories