App Registeries Not ready

20 views
Skip to first unread message

Vikrant Gupta

unread,
Aug 15, 2018, 8:52:34 AM8/15/18
to Django users
I'm trying to get a name from a column named Artist. But got an error in using get().
 The code is as follows...
from django.db import models
# Create your models here.


class Artist(models.Model):
name=models.CharField('artist',max_length=50,primary_key=True)
year_formed=models.PositiveIntegerField()

class Album(models.Model):
name=models.CharField('album',max_length=50)
artist=models.ForeignKey(Artist,on_delete=models.PROTECT)

from polls.models import Artist

newartist=Artist(name='Vikrant',year_formed=1985)
newartist.save()

artist=Artist.objects.all()
for i in artist:
print(i.name)
print(i.year_formed)

ind=Artist.objects.get(id=1)
print(ind.name)
Screen Shot 2018-08-15 at 6.15.52 PM.png

Jason

unread,
Aug 15, 2018, 9:08:20 AM8/15/18
to Django users
I suggest you do that in the django shell in the terminal, not in the model code. 

Vikrant Gupta

unread,
Aug 15, 2018, 9:12:00 AM8/15/18
to django...@googlegroups.com
Sure. May I know what’s the reason not to use in model code? 
So How can I develop my project?

With thanks ,
Vikrant


On Aug 15, 2018, at 6:38 PM, Jason <jjohn...@gmail.com> wrote:

I suggest you do that in the django shell in the terminal, not in the model code. 

--
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/bcd68cfc-fe9e-4eb5-bfd9-35c4a77d53d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shashank Parekh

unread,
Aug 15, 2018, 9:42:50 AM8/15/18
to Django users
Can you post  your apps.py file?

Vikrant Gupta

unread,
Aug 15, 2018, 9:46:04 AM8/15/18
to Django users
Yeah... Here it's

from django.apps import AppConfig


class PollsConfig(AppConfig):
name = 'polls'

Kasper Laudrup

unread,
Aug 15, 2018, 12:47:47 PM8/15/18
to django...@googlegroups.com
Hi Vikrant,

On 15/08/2018 15.11, Vikrant Gupta wrote:
> Sure. May I know what’s the reason not to use in model code?

I'm far from being an expert in Django, so I don't know the details, but
I'm sure Django does quite a lot of "magic" when you write a class that
inherits from models.Model which is most likely the cause of the error
you see.

Apart from the actual issues you're having, I think it's a lot cleaner
design to stick to defining model classes in models.py (or wherever you
declare your models) and avoid writing any code there that is evaluated
at import time.

In general I think it's better to avoid having code being evaluated at
import time unless you have a very good reason for that (and there are
tons of those, but as a rule of thumb).

> So How can I develop my project?
>

I assume you're trying to debug your models or just taking things step
by step, which is a very good way to approach things IMO.

Jason suggested you could you use the shell, which is definitely one
good option for testing things out.

I would probably write a very simple view, move the code you posted
there and access that from your browser. The view might not really
return anything, or even fail, but assuming you're going to use your
models in that view anyway, it's a good way to see (in your console
output) that something works before you start passing model instances to
render templates etc.

You could also start writing unittests for your models, views etc. which
is something I highly recommend doing no matter what, but that's
probably out of scope for now :-)

Just my 2 cents.

Kind regards,

Kasper Laudrup

Vikrant Gupta

unread,
Aug 15, 2018, 12:52:16 PM8/15/18
to django...@googlegroups.com
Thank you so much for patiently explanation on my question. I’ll try to do it.

With thanks,
Vikrant
> --
> 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/8ecf7080-1b2e-aefc-7c2a-94952e2bf5cc%40stacktrace.dk.

Vikrant Gupta

unread,
Aug 16, 2018, 5:13:37 AM8/16/18
to django...@googlegroups.com
What’s the code for importing pymysql database in Django manage.py ?

-- 
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.
Reply all
Reply to author
Forward
0 new messages