Add value to database without using forms in django

23 views
Skip to first unread message

Mitul Tyagi

unread,
Sep 30, 2017, 5:39:53 AM9/30/17
to Django users
I have a model named "Name" which stores the list of all cities present in other model named  "City". How can it be done internally in the views.py without using forms. Here is the code of models.py

"
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models


class Detail(models.Model):
    username = models.CharField(max_length=100)
    password = models.CharField(max_length=100)

    def __str__(self):
        return str(self.username)


class City(models.Model):
    userName = models.ForeignKey(Detail, blank=True, null=True)
    city1 = models.CharField(max_length=100)
    city2 = models.CharField(max_length=100)
    city3 = models.CharField(max_length=100)
    city4 = models.CharField(max_length=100)
    city5 = models.CharField(max_length=100)
    def __str__(self):
        return "Id No:" + str(self.pk)+" and Name: "+str(self.userName)

class Name(models.Model):
    cityval=models.CharField(max_length=100)
"

Constantine Covtushenko

unread,
Sep 30, 2017, 9:10:45 AM9/30/17
to django...@googlegroups.com
Hi Mitul,

Can you clarify a little bit more your question?
What are you trying to resolve?
You asked: "How it will be done...?" It is not clear what exactly you are trying to be done.

I guess that it my be saving cities into model 'cityval' fields. Am I correct?

If so you can do something like that:

Name(cityval=''.join([city.city1, city.city2, ...])).save()

Regards,
Constantine C.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5d83ea00-e613-4b86-830f-262b1db4ce99%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Sincerely yours,
Constantine C

Derek

unread,
Sep 30, 2017, 11:49:41 AM9/30/17
to Django users
Yes, its a strange question.

You can of course always add a new method to the City class:

class City(models.Model):

   ...

    def get_cities(self):
        return ''.join([self.city1, self.city2, self.city3, self.city4, self.city5]))

and then store those in Name; but from a design POV, probably 
best not to store them but just get them when you need them.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Mitul Tyagi

unread,
Sep 30, 2017, 12:07:50 PM9/30/17
to Django users
Yup did it.....save() method. I wanted to know if we need to create form for saving or other way's are present also...I got the answer. Thanks for the reply
Reply all
Reply to author
Forward
0 new messages