I am trying to enter data using the model in the sqlite3, but I am facing a problem in determining the type of this data.
Here is the dataframe:
enter image description here
I have written the following script:
from django.db import models
from django.utils.translation import gettext as _
# Create your models here.
class Norme (models.Model):
numdos = models.CharField(_("NUMDOS"), max_length=255)
numdosverling = models.CharField(_("NUMDOSVERLING"),
max_length=255)
ancart = models.RegexField(_("ANCART"))
filiere = models.CharField(_("FILIERE"))
etape = models.FloatField()(_("ETAPE"))
verling = models.CharField(_("VERLING"))
formats = models.CharField(_("FORMAT"))
When I run the code: .mode csv, then .import data.csv name_of_model
I obtained this result:
enter image description here
I have tried to change the type of database but I still get the same result.
Related
I'm pretty new to django and I'm stuck at the problem with models. Here is my users app's models file:
from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MinValueValidator, MaxValueValidator
from django.urls import reverse
from PIL import Image
class Schedule(models.Model):
title = models.CharField(max_length=150)
context = models.TextField()
class Input(models.Model):
DAYS_OF_FITNESS = [
('month', '30'),
('month_2', '60'),
('month_3', '90'),
]
weight = models.PositiveIntegerField(validators=[MinValueValidator(40)])
age = models.PositiveIntegerField(validators=[MinValueValidator(12)])
days = models.CharField(
max_length=20, choices=DAYS_OF_FITNESS, default='30')
athlete = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
schedule = models.ForeignKey(Schedule, on_delete=models.CASCADE)
I'm trying to link each user with its input using OneToOneField and Schedule is linked as a foreign key to Input model
But when I'm trying to migrate models, I'm getting this error:
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: users_input
I checked that Both models are in django settings, and I migrated them. BTW, I'm using signals to save inputs automatically, (just for note if it helps)
It's because that table doesn't exist in the database. To create tables you have to do the following. Execute the following commands in your terminal.
python manage.py makemigrations
python manage.py migrate
I can't import a CSV file into model on Django.
I made a column 'author' and put the superuser's id which I am login to the admin site.
But there was an error like this when I import the CSV file.
Line number: 1 - null value in column "author_id" violates not-null constraint DETAIL: Failing row contains (10, abc, blahblah, null, ).
5, abc, blahblah, , nah,wha,blah
csv file
author,title,text,file,free_tags
5,abc,blahblah,,"nah,wha,blah"
models.py
from django.db import models
from django.urls import reverse
from taggit.managers import TaggableManager
class KnowHow(models.Model):
author = models.ForeignKey('auth.User',on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField(blank=True)
file = models.FileField(blank=True,upload_to='explicit_knowhows')
free_tags = TaggableManager(blank=True)
def __str__(self):
return self.title
admin.py
from django.contrib import admin
from import_export import resources
from import_export import fields
from import_export.admin import ImportExportModelAdmin
from .models import KnowHow
# Register your models here.
class KnowHowResource(resources.ModelResource):
class Meta:
model = KnowHow
exclude = 'id'
import_id_fields = ('title', )
#admin.register(KnowHow)
class knowHowAdmin(ImportExportModelAdmin):
resource_class = KnowHowResource
The error says that author_id is missed.
Django adds a postfix to all the ForeignKey fields, so you should try to modify the file renaming the column:
author_id,title,text,file,free_tags
5,abc,blahblah,,"nah,wha,blah"
It fixed when I saved the CSV by encoding in UTF-8. This will not support non-alphabet letter so I recommend using .xlsx file instead.
Thank you to everyone who tried to fix my problem.
I'm trying to import a model from another Django app in my project. However, when I try to import, I keep getting an error for:
ImportError No module named trunk.profiles.models.
However, when I cmnd click on the model on my IDE it takes me to the model. So it recognizes where the model is coming from, but I think for some reason Django is not recognizing the path.
Here is my code from my models.py which I'm trying to import another model, Profiles from a different Django app:
from django.db import models
from trunk.profiles.models import Profiles # source of error
class ContentObject(models.Model):
course_name = models.CharField(max_length15)
course_topic = models.CharField(max_length = 30)
op_UserName = models.ForeignKey(Profiles)
Add trunk.profiles to Your INSTALLED_APPS
settings.py
INSTALLED_APPS = [
...
'trunk.profiles'
]
TIP
Instead of import model, specify a model with the full application label
from django.db import models
class ContentObject(models.Model):
course_name = models.CharField(max_length15)
course_topic = models.CharField(max_length = 30)
op_UserName = models.ForeignKey('trunk.profiles.Profiles')
I have a model for movies in my django project:
class Movie(models.Model):
title = models.CharField(max_length=128, blank=False)
description = models.TextField()
movie_length = models.CharField(max_length=20)
director = models.CharField(max_length=50)
rating = models.FloatField(validators=[MinValueValidator(0.0),MaxValueValidator(10.0)])
created_at = models.DateTimeField(auto_now_add=True)
image = models.ImageField(upload_to='movie_img/')
category = models.CharField(max_length=50)
I want to fill the database using python API wrappers for themoviedb.org API like this https://github.com/celiao/tmdbsimple/ or https://github.com/wagnerrp/pytmdb3/. But i am not sure how to do this. I just want to put around 100 movies at one go without having to add every movie one by one.
How can i do this, please help.
Thank you.
First you will have to read the data from the API into Python objects. Assume you now have a list L of tuples, each tuple is a record of a movie.
Your script could be load_from_api.py
#!/usr/bin/env python
import os
import sys
# ... read the data from the API
# ... so L = [("movie title","movie description..."),("movie2 title",..)]
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
import django
django.setup()
from yourapp.models import Movie
for movie in L:
m = Movie(title=movie[0],
description=movie[1],
...
)
m.save()
Then simply run the script.
Note that you could load the data directly to the DB with SQL, but that is not recommended since this ignores the django models rules, defaults, validations, signals etc.
I am trying to setup an user-uploaded class through the backend as Django admin. I am trying to make it so the path of the ImageField is based on the user-inputted DateField information; this is what I have.
from django.db import models
from datetime import date
class Article(models.Model):
def img_date(self):
return str(self.date.year) + str(self.date.month) + str(self.date.day)
#main_title = models.
title = models.TextField(max_length=200)
date = models.DateField()
content = models.TextField()
link = models.CharField(max_length=200)
image = models.ImageField(upload_to=img_date)
However, when I submit the object, I get an error saying "img_date() takes 1 positional argument but 2 were given". I need some help figuring out how to set a manual path like I explained earlier.
Thanks,
Have a look at the FileField docs (ImageField inherits from FileField).
In particular, note that the upload_to callable must accept two arguments, the model instance, and the original file name. So your code could look something like this (I removed the date import because it was unused):
from django.db import models
def img_date(instance, filename):
return str(instance.date.year) + str(instance.date.month) + str(instance.date.day)
class Article(models.Model):
title = models.TextField(max_length=200)
date = models.DateField()
content = models.TextField()
link = models.CharField(max_length=200)
image = models.ImageField(upload_to=img_date)
I've used your example code but you'll probably want to modify it so that two articles with the same date don't use the same image path.