Saturday, September 30, 2017

Re: Add value to database without using forms in django

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.


On Saturday, 30 September 2017 15:10:45 UTC+2, Constantine Covtushenko wrote:
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.

On Sat, Sep 30, 2017 at 5:39 AM, Mitul Tyagi <mitult...@gmail.com> wrote:
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)
"

--
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...@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

--
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-users@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/a51afb41-e8b5-4431-a530-28ba500df3f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment