nexla
nexla

Reputation: 453

Django doesn't create object in database

I am not sure what's wrong.
I have tried migrate/makemigrations and that doesnt help, Im working in virtualenv tho.
my models.py

    from django.db import models
import re


# Create your models here.
class Currency(models.Model):
    def __str__(self):
        return self.short
    name = models.CharField(max_length=32, null=True, blank=True)
    country = models.CharField(max_length=50, null=True, blank=True)
    short = models.CharField(max_length=10, null=True, blank=True)


class Source(models.Model):
    def __str__(self):
        return self.name + '({}...)'.format(self.url[:20])
    url = models.URLField()
    name = models.CharField(max_length=250, verbose_name='Source_name')
    timestamp = models.DateTimeField(auto_now_add=True)
    currency = models.ForeignKey(Currency)

    def save(self, *args, **kwargs):
        self.name = ''.join(re.sub('\s+', ' ', self.name).strip())
        super(Source, self).save()


class Product(models.Model):
    def __str__(self):
        return self.name
    name = models.CharField(max_length=250, blank=False)
    prices = models.ManyToManyField(to='Price', blank=True)
    date_creation = models.DateField(auto_now_add=True)
    prod_code = models.CharField(max_length=250, blank=True, null=True)

    def save(self, *args, **kwargs):
        self.name = ''.join(re.sub('\s+', ' ', self.name).strip())
        super(Product, self).save()  

My function that creates objects

def from_csv_to_db(csv_name):  # empty DB
try:
    df = pd.read_csv(csv_name, sep=';')
except FileNotFoundError:
    return ('Please input correct path to csv file or check spelling. Process finished')
for i, row in df.iterrows():
    Currency.objects.get_or_create(short=row['source__currency__short'], id=row['source__currency_id'])
    Product.objects.get_or_create(id=row['product__id'], name=row['product__name'])
    Source.objects.get_or_create(id=row['source__id'], name=row['source__name'], url = row['source__url'],
                                 currency_id=row['source__currency_id'])
    Parser.objects.get_or_create(script=row['script'], boolean=row['boolean'], product_id=row['product__id'],
                                 source_id=row['source__id'])
return 'Done'

However, django does display my models object via queryset in python manage.py shell

from Parser.models import *
Product.objects.all()  
<QuerySet [<Product: test product>, <Product: iphone>, <Product: 
Mac>, <Product: iPad>]>  

however when I run python manage.py dbshell > select * from Parser_product;
I get ERROR: relation "parser_product" does not exist

I use Docker on Debian and this is done remotely.

Upvotes: 1

Views: 1461

Answers (2)

nexla
nexla

Reputation: 453

Gladly I found the solution via python chat in telegram.

The answer would be a raw query

I had to make it SELECT * FROM "Parser_product" < note the quotes.

Thanks to this answer

Upvotes: 1

user1800722
user1800722

Reputation:

Try running the makemigration command along with the app name. This is needed if you have models in the default app. Eg - python manage.py makemigrations parser. Then run the migrate command.

Upvotes: 0

Related Questions