Quantcast

Mysql/Models

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Mysql/Models

alkhairohr
Hello everyone,

Again, I'm super new to django so please bare with me. 

I have just connected my mysql database to my django project in settings.py. I then manually made models for the tables in my database. I originally used inspectdb to do this, but I was getting a bunch of errors. I decided to just do it manually from there, although I'm not necessarily sure if it is correct yet. Do my models have to perfectly match my sql relations? My tables already have unique ids as a field. I now that django automatically does this, that is, make unique ids? Will this be a problem. 

Anyway, I then ran makemigrations and migrate (which worked wonderfully), and now I am trying to simply view some data from my database in the manage.py shell. I am not exactly sure how to do this, so any help would be great. Further, am I missing any steps as far connecting the mysql database to my django project? Lastly, a rather huge disconnect I am having with all of this is simply understanding how exactly the models.py in my apps are actually connected to my database?

Here are a few snapshots of my files:

Models.py 

   

manage.py shell


--
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 [hidden email].
To post to this group, send email to [hidden email].
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/6f71bcb1-505b-462a-87f8-5a0402419b50%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mysql/Models

comeon


On Apr 17, 2017 12:57 AM, [hidden email] wrote:
>
> I am not exactly sure how to do this, so any help would be great.
The shell is just a Python REPL, you can run any Python code in it. Consider:
>>> from yourapp import models
>>> models.YourModel.objects.first()
<YourModel: YourModel object>
>Further, am I missing any steps as far connecting the mysql database to my django project?
If you can do the above successfully I don't think so.
>Lastly, a rather huge disconnect I am having with all of this is simply understanding how exactly the models.py in my apps are actually connected to my database?
Short answer: they're not really.
Long answer: When you try to access a field of a model in Django, Django will query the table related to that model, get the row with the ID of your model, then give you the correct column.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/58f44f01.57e41c0a.fbbee.ad3bSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mysql/Models

Camilo Torres
In reply to this post by alkhairohr
Hi,

You can use a Meta class for your models and add a db_table property to control the table name related to the model, that way, you can name your model as you like (and differently from the DB table name).
You can also use the db_column parameter to Fields to control the name of the column in the table you are associating the model fields. That allows you to use lowercase field names as customary for Python, for example:

class Organism(models.Model):
    author = models.CharField(max_length=200, db_column='Author')
    ...


Please, read this documentation:
https://docs.djangoproject.com/en/1.11/ref/models/options/
https://docs.djangoproject.com/en/1.11/ref/models/fields/#db-column


On Sunday, April 16, 2017 at 6:01:16 PM UTC-4, [hidden email] wrote:
Hello everyone,

Again, I'm super new to django so please bare with me. 

I have just connected my mysql database to my django project in settings.py. I then manually made models for the tables in my database. I originally used inspectdb to do this, but I was getting a bunch of errors. I decided to just do it manually from there, although I'm not necessarily sure if it is correct yet. Do my models have to perfectly match my sql relations? My tables already have unique ids as a field. I now that django automatically does this, that is, make unique ids? Will this be a problem. 

Anyway, I then ran makemigrations and migrate (which worked wonderfully), and now I am trying to simply view some data from my database in the manage.py shell. I am not exactly sure how to do this, so any help would be great. Further, am I missing any steps as far connecting the mysql database to my django project? Lastly, a rather huge disconnect I am having with all of this is simply understanding how exactly the models.py in my apps are actually connected to my database?

Here are a few snapshots of my files:

Models.py 

  <a href="https://scontent.xx.fbcdn.net/v/t34.0-12/17909331_10208121547186599_508598864_n.png?oh=1c21c01e48b70f99479c3a915eaa1161&amp;oe=58F6563E" style="text-align:center;clear:left;margin-bottom:1em;margin-right:1em;display:inline!important" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fscontent.xx.fbcdn.net%2Fv%2Ft34.0-12%2F17909331_10208121547186599_508598864_n.png%3Foh%3D1c21c01e48b70f99479c3a915eaa1161%26oe%3D58F6563E\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHEJJDV9gm09a2r5170Od9RzdoRHg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fscontent.xx.fbcdn.net%2Fv%2Ft34.0-12%2F17909331_10208121547186599_508598864_n.png%3Foh%3D1c21c01e48b70f99479c3a915eaa1161%26oe%3D58F6563E\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHEJJDV9gm09a2r5170Od9RzdoRHg&#39;;return true;"> 

manage.py shell

<a href="https://scontent.xx.fbcdn.net/v/t34.0-12/17909331_10208121547186599_508598864_n.png?oh=1c21c01e48b70f99479c3a915eaa1161&amp;oe=58F6563E" style="clear:left;margin-bottom:1em;float:left;margin-right:1em" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fscontent.xx.fbcdn.net%2Fv%2Ft34.0-12%2F17909331_10208121547186599_508598864_n.png%3Foh%3D1c21c01e48b70f99479c3a915eaa1161%26oe%3D58F6563E\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHEJJDV9gm09a2r5170Od9RzdoRHg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fscontent.xx.fbcdn.net%2Fv%2Ft34.0-12%2F17909331_10208121547186599_508598864_n.png%3Foh%3D1c21c01e48b70f99479c3a915eaa1161%26oe%3D58F6563E\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHEJJDV9gm09a2r5170Od9RzdoRHg&#39;;return true;">

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/a0345dda-370e-40f8-8e68-0fbf37e4c422%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...