0

I am new to Django and I need some help. I am trying to make sqlite3 database with Djnago. I have created class in models.py, which looks like this:

 class Data(models.Model):
       vendor_name = models.CharField(max_length=100)
       product_name = models.CharField(max_length=100)
       versions = models.TextField()
       description = models.TextField()
       date = models.DateTimeField(default=timezone.now)

I am trying to insert list of data in field named versions. List looks like this:

['8.0.1', '8.0.2', '8.0.3', '8.1.2']

At the end I would like to check, if list contains specific value, for example I would like to check if list contains 8.1.2. If value is in list it should return True. I am able to insert data in database like this:

   data = Data(vendor_name='paloalto', product_name='PA', versions=['8.0.1', '8.0.2', '8.0.3', '8.1.2'], description='...')
   data.save()

I do not know how to check if database contain specific value in versions field, I will appreciate any help.

0

1 Answer 1

2

Add a new entity Versions. Then make a 1 to n relationship between Data and Versions

class Data(models.Model):
   vendor_name = models.CharField(max_length=100)       

class Versions(models.Model):
     data = models.ForeignKey(Data)
     version = models.CharField()

def query():
    Versions.objects.filter(data__vendor_name='lala', version=3)
Sign up to request clarification or add additional context in comments.

2 Comments

How can I insert values from the list to the table?
You crate Version objects and save them. You have 2 database tables now. Read a bit about relationships in the django models docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.