Hi I am trying to insert data to the database in django without forms.
This is my views.py file
def updatetrans(request):
json_data=open('/home/ttt/Abc/a.json').read()
data = json.loads(json_data)
for pk, pv in data.iteritems():
for k,v in pv.iteritems():
try:
print k, " =>> ", pv['transcript'][1]
except:
pass
This is my url.py file
url(r'^updatetrans/$', 'booki.account.views.updatetrans', name='updatetrans'),
Here is my models.py file. I have created two tables. And want to insert data in both of them separately.
class TransType(models.Model):
name = models.TextField()
def __unicode__(self):
return self.name
class Trans(models.Model):
trans = models.ForeignKey(TransType)
script = models.CharField(max_length=200)
def __unicode__(self):
return self.trans
I am getting the output on console. That output I want to save to the database.
Plz help.
Thank you.. I tried some other way. I am getting error as:
global name 'TransType' is not defined
Not inserted ==> e
My code:
def updatetrans(request):
json_data=open('/home/ttt/Ali/a.json').read()
data = json.loads(json_data)
for pk, pv in data.iteritems():
for k,v in pv.iteritems():
try:
trans_type = TransType.objects.get_or_create(name=k)
trans = Trans()
trans.trans_id = trans_type.id
trans.script = pv[k][1]
trans.save()
print " Inserted ==>", pv[k][1]
except Exception, e:
print e
print "Not inserted ==>", pv[k][1]
pass
return HttpResponse("Done")
The problem is solved. The answer is as follows.
To Store the records in the django database without using any input or form.
To avoid duplicate entries in the database.
This is my views.py file
def updatetrans(request):
json_data=open('/home/ttt/Ali/a.json').read()
data = json.loads(json_data)
for pk, pv in data.iteritems():
for k,v in pv.iteritems():
try:
trans_type = TransType.objects.get_or_create(name=k)
trans = Trans()
trans.transtype_id = trans_type[0].id
if isinstance(pv[k], basestring):
script = pv[k]
else:
print "****** List ****"
script = pv[k][1]
trans.script = script
trans.save()
print " Inserted ==>", script
except Exception, e:
print e
#print "Not inserted ==>", pv[k][1]
pass
return HttpResponse("Done")
This is my models.py file.
class TransType(models.Model):
name = models.TextField()
created = models.DateTimeField(auto_now_add = True)
updated = models.DateTimeField(auto_now = True)
def __unicode__(self):
return self.name
class Trans(models.Model):
transtype = models.ForeignKey(TransType)
script = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add = True)
updated = models.DateTimeField(auto_now = True)
class Meta:
unique_together = (("transtype", "script"),)
def __unicode__(self):
return self.trans
You just want to save data to database, so you can do it like this easily
>> cd project_directory
>> python manage.py shell
>> from xxx.models import TransType,Trans
>> tt = TransType.objects.create(name='read from file')
>> Trans.objects.create(trans=tt, script='read from file')
or write a python script to import data to database, put it in your project directory,run python manage.py shell then import yourscript
if you don't like python manage.py shell, just set DJANGO_SETTINGS_MODULE environment, then just run python yourscript in terminal. Such as
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "yoursite.settings"
# The above two lines could be written simply as:
# from project.wsgi import *
from xxx.models import import TransType,Trans
TransType.objects.create()
Trans.objects.create()
remember to replace xxx with your app name
see QuerySet API:https://docs.djangoproject.com/en/dev/ref/models/querysets/#create
Chinese people could see here (other people could just read the code): http://www.ziqiangxuetang.com/django/django-import-data.html
You can do it using Model.objects.create()
Let's say you're receiving data from post form and want to save in QuillModel, here's how to do it in python2 django
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import redirect, render
from .forms import TemplateForm
from .models import QuillModel
def app3(request):
if request.method == "POST":
print(request.POST)
html_content = request.POST.get('html_content')
input_area = request.POST.get('input_area')
if html_content and input_area:
obj = QuillModel.objects.create(html_content=html_content, input_area=input_area)
obj.save()
return redirect('/app3/app3')
else:
form = TemplateForm()
return render(request, 'app3/forms/f1_post_form.html', {'form' : form})
See the if request.method == "POST": part for saving into database.
Since I have been doing the same thing..
For example:
models.py
class Dataset(models.Model):
hash = models.CharField(max_length=32)
category = models.CharField(max_length=10)
views.py
if request.method == "POST":
uploaded_file = request.FILES['document']
fs = FileSystemStorage()
fs.save(uploaded_file.name,uploaded_file)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
media_path = os.path.join(BASE_DIR,'dataset')
full_path=os.path.join(media_path,uploaded_file.name)
f = default_storage.open(full_path, 'r')
data = f.read()
for i in data.split("\n"):
hash,category = i.strip("\n").split(",")
Dataset.objects.create(hash=hash,category=category)
print("yes")
f.close()
Conclusion
You just specify your models and then create with what variable or column that you have.
MODELS.objects.create(column1=data1,column2=data2)
Example inside of my file
12345678,good
12345678,bad
Related
So I am extremely new to programming, and am stuck at this issue, I am using python with Django and Mongodb for database. I need to write a service that assigns an ID (not the one assigned by mongodb) upon each user form submission. for example entry 1's ID will be [Prefix entered by user] 2101, entry 2's ID will be [Prefix entered by user] 2102, so its basically adding in the number 2100.
I have no idea how and where to integrate this logic in my code. I have tried a few solutions on the internet but nothing seems to work.
my code:
Model.py
class Writeups(Document):
blog_id = 2100
title = fields.StringField(max_length=120)
date_created = fields.DateField(blank=True, null=True)
date_modified = fields.DateField(blank=True, null=True)
version_number = fields.DecimalField(null= True , max_digits=1000, decimal_places=2)
storage_path = fields.StringField(max_length=120)
STRIKE_READY_BRIEF = 'SRB'
STRIKE_READY_THREAT_REPORT = 'SRTR'
PREFIX_CHOICES = [
(STRIKE_READY_BRIEF, 'SRB'),
(STRIKE_READY_THREAT_REPORT, 'SRTR'),
]
prefix = fields.StringField(
max_length=4,
choices=PREFIX_CHOICES,
null=False,
blank=False,
)
views.py:
#csrf_exempt
def writeups_request(request):
"""
Writeup Request
"""
if request.method == 'GET':
try:
data = {
'form-TOTAL_FORMS': '1',
'form-INITIAL_FORMS': '0',
}
writeups = WriteupsFormset(data)
# print(writeup)
return render(request, "writeups/writeups.html", {'writeups_forms': writeups})
except Exception as e:
print(e)
response = {"error": "Error occurred"}
return JsonResponse(response, safe=False)
if request.method == 'POST':
writeup_data = WriteupsFormset(request.POST)
if writeup_data.is_valid():
flag = False
logs = []
for writeups_data in writeup_data:
print(writeups_data)
if writeups_data.cleaned_data.get('DELETE'): # and malware_data._should_delete_form(form):
continue
title = writeups_data.cleaned_data.get('title')
date_created = writeups_data.cleaned_data.get('date_created')
date_modified = writeups_data.cleaned_data.get('date_modified')
version_number = writeups_data.cleaned_data.get('version_number')
storage_path = writeups_data.cleaned_data.get('storage_path')
prefix = writeups_data.cleaned_data.get('prefix')
try:
writeups = Writeups(),
title=title,
date_created=date_created,
date_modified=date_modified,
version_number=version_number,
storage_path=storage_path,
prefix=prefix)
writeups.save()
In order to implement your custom script at all submissions, Django's Signals are the solution for your use case. Look into post_save and pre-save signals and use them as per your problem.
https://docs.djangoproject.com/en/3.2/ref/signals/
If you have an existing database and you want a script to iterate through it and update the dataset, you can take a look at Management Commands
https://docs.djangoproject.com/en/4.0/howto/custom-management-commands/
I am new to Django, and I'd like to ask some questions
I used the model layer to create the model with fields in it,it can create user_id,movie_id field in the database.
class Rating(models.Model):
user_id = models.CharField(max_length=16)
movie_id = models.CharField(max_length=16)
rating = models.DecimalField(decimal_places=2, max_digits=4)
rating_timestamp = models.DateTimeField()
type = models.CharField(max_length=8, default='explicit')
def __str__(self):
return "user_id: {}, movie_id: {}, rating: {}, type: {}".format(self.user_id, self.movie_id, self.rating, self.type)
class Cluster(models.Model):
cluster_id = models.IntegerField()
user_id = models.IntegerField()
def __str__(self):
return "{} in {}".format(self.user_id, self.cluster_id)
Execute this statement using Python xxx.py,I want to populate the database but it doesn't work
import os
import urllib.request
import django
import datetime
import decimal
from tqdm import tqdm
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()
from analytics.models import Rating
def create_rating(user_id, content_id, rating, timestamp):
rating = Rating(
user_id=user_id, movie_id=content_id,
rating=decimal.Decimal(rating),
rating_timestamp=datetime.datetime.fromtimestamp(
float(timestamp)
)
)
rating.save()
return rating
def download_ratings():
URL = 'https://gitee.com/zhangyoushang/wokkk/blob/master/latest/ratings.dat'
response = urllib.request.urlopen(URL)
data = response.read()
print('download finished')
return data.decode('utf-8')
def delete_db():
print('truncate db')
Rating.objects.all().delete()
print('finished truncate db')
def populate():
delete_db()
ratings = download_ratings()
for rating in tqdm(ratings.split(sep="\n")):
r = rating.split(sep="::")
if len(r) == 4:
create_rating(r[0], r[1], r[2], r[3])
if __name__ == '__main__':
print("Starting MovieGeeks Population script..." )
populate()
But I found that my database was empty and there was no data in it.
No error is reported, execute as follows:
I making a email delivery WebApp, I have an Email Class with uploadcsvfile to upload the .csv contact file from the user's computer to server at location CONTACT_DIR, what I'm unable to figure out is how to get the saved contacts_some_char.csv name in my views.py, the purpouse of getting it's name is so that I can parse the email list and send email to them.
I have my model.py as follows :
class Email(models.Model):
fromemail = models.EmailField(blank = False, max_length = 254)
uploadcsvfile = models.FileField(upload_to = 'Contacts', blank = False)
subject = models.CharField(blank=False,max_length = 254)
bodyHeading = models.CharField(blank = False,max_length = 254)
bodytext = models.TextField(blank = False)
name = models.CharField(blank = False,max_length = 254)
Here is my views.py :
def index(request):
if request.method == 'POST':
email_form = EmailForm(request.POST, request.FILES)
if email_form.is_valid():
email_form.save(commit = True)
print(Email.uploadcsvfile)
contacts = Contacts()
#path = settings.CONTACT_DIR
f = open(settings.CONTACT_DIR+"/"+str(uploaded_file_name))
csv_f = csv.reader(f)
Emails = []
Names= []
L_Names = []
for x in csv_f:
Emails.append(x[0])
Names.append(x[1])
L_Names.append(x[2])
f.close()
contacts.ListOfEmails = json.dumps(Emails)
contacts.ListOfFirstNames = json.dumps(Names)
contacts.ListOfLastNames = json.dumps(L_Names)
contacts.save()
Here is forms.py for the same :
from django import forms
from dcsvreader.models import Email
class EmailForm(forms.ModelForm):
class Meta():
model = Email
fields = "__all__"
A.K.A How do i get uploaded_file_name in views.py, I'm reading in various QnA at S.O that this is related to FileDescriptor but can't find how exactly also, print(Email.uploadcsvfile) gives <django.db.models.fields.files.FileDescriptor object at 0x03F7DF90> as output.
request.FILES is a dictionary-like object containing all uploaded files and the uploadcsvfile is the key in which you are making a POST request. Hence, request.FILES['uploadcsvfile'] will hold the particular file as InMemoryUploadedFile and it has a variable .name which holds the filename
Try this,
file_name = request.FILES['uploadcsvfile'].name
Reference
uploadfile.name
request.FILES
I'm pretty new to django myself but it looks like here:
print(Email.uploadcsvfile)
You're trying to print a class attribute rather than an attribute of an instance.
Have you tried the following?
print(email_form.uploadcsvfile.name)
I'm using DJango 1.8 and Python 3.4
When the below view is being ran, Django throws Type Error - Object is not JSON Serializable
Views.py
from django.http import HttpRequest,HttpResponse
from django.http import JsonResponse
from json import dumps
def get_stats(request):
if request.method == "POST":
srch_dropV = request.POST['srch_dropAJ']
else:
srch_dropV = ''
if(srch_dropV == 'Green'):
students = GreenBased.objects.all()
if(srch_dropV == 'Yellow'):
students = YellowBased.objects.all()
response_data = {}
try:
response_data['result'] = 'Success'
response_data['message'] = list(students)
except:
response_data['result'] = 'Ouch!'
response_data['message'] = 'Script has not ran correctly'
return HttpResponse(JsonResponse(response_data), content_type="application/json")
I'm trying to read couple of rows from mysql database and display it on the html file, I'm facing below error message when above view is being ran
TypeError: YellowBased: YelloBased object is not JSON serializable
In HTML Page, I have a drop down list.. based on the option that is selected, my Ajax would return me the records that were fetched from mysql table.
Models.py
class GreenBased(models.Model):
NumOfStudents = models.CharField(max_length=300,blank=True)
Green = models.CharField(max_length=300,blank=True)
class Meta:
managed = False
db_table = "GreenStats"
class YelloBased(models.Model):
NumOfStudents = models.CharField(max_length=300,blank=True)
Yellow = models.CharField(max_length=300,blank=True)
class Meta:
managed = False
db_table = "YellowStats"
GreenStats and YellowStats tables contains only 2*2 rows in mysql
Can someone please help me to identify this issue ?
You have to serialize your student objects list, try something like this:
from django.http import HttpRequest,HttpResponse
from django.http import JsonResponse
from json import dumps
from django.core import serializers
def get_stats(request):
if request.method == "POST":
srch_dropV = request.POST['srch_dropAJ']
else:
srch_dropV = ''
if(srch_dropV == 'Green'):
students = GreenBased.objects.all()
if(srch_dropV == 'Yellow'):
students = YellowBased.objects.all()
response_data = {}
try:
response_data['result'] = 'Success'
response_data['message'] = serializers.serialize('json', students)
except:
response_data['result'] = 'Ouch!'
response_data['message'] = 'Script has not ran correctly'
return JsonResponse(response_data)
Notice the line change in :
response_data['message'] = serializers.serialize('json', students)
Also JsonResponse does the trick on its own, so no need to wrap it in a HttpResponse.
check the docs for more customization:
https://docs.djangoproject.com/en/1.8/topics/serialization/
Hope this helps!
After so much googling I did'nt found any relevant solution for getting the associated models data in views.
Things is I am requesting to the server via Ajax and want to fetch the folders and folder creator name(from User model). But it did'nt return me the first_name of the creator. Here's the code
models.py
class UserFolder(models.Model):
creator = models.ForeignKey(User)
name = models.CharField(blank=False, max_length=150)
is_shared = models.BooleanField(default=False)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
views.py
def pagination(obj, request):
max = request.GET.get('max') if 'max' in request.GET else 1
paginator = Paginator(obj, max) # Show 25 contacts per page
page = request.GET.get('page')
try:
return paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
return paginator.page(1)
except EmptyPage:
return ""
def folder_list(request): #called via ajax
folders = UserFolder.objects.filter(creator=request.user.id).order_by('-modified_date')
folders = pagination(folders, request) #for pagination
folders = json.dumps({}) if not folders else serializers.serialize("json", folders)
return HttpResponse(folders)
I have tried .select_related('creator') too but it did'nt work.
In js I am fetching the data like this :
$.each(data, function(i, v) {
var t = fldrTpl, id = v.pk;
v = v.fields
t = t.replace(/\{(ID)\}/g, id);
t = t.replace(/\{(NAME)\}/g, v.name);
t = t.replace(/\{(C_SIZE)\}/g, (v.current_size == null?0:v.current_size));
t = t.replace(/\{(C_ID)\}/g, v.creator.first_name);
t = t.replace(/\{(C_DATE)\}/g, v.created_date);
$(".my-folder-table").append(t);
});
v.creator.first_name always return undefined.
Any help would be really appreciated.
Thanks
You can always create a dictionary of the data you want to use on your client side:
def folder_list(request): #called via ajax
folders_obj = UserFolder.objects.filter(creator=request.user).select_related('creator').order_by('-modified_date').all()
folders_dict = []
for folder in folders_obj:
d = {}
for k, v in folder.__dict__.items():
d[k] = str(v)
d["creator__first_name"] = folder.creator.first_name
folders_dict.append(d)
folders = pagination(folders_dict, request) #for pagination
folders = json.dumps({}) if not folders else serializers.serialize("json", folders)
return HttpResponse(folders)
And in your js:
t = t.replace(/\{(C_ID)\}/g, v.creator__first_name);