The inline foreign key did not match the parent instance primary key.

2015-05-09 Thread Ajay Kumar
I'm trying to set custom id for my model,

*models.py*

from django.db import models
import uuid


def get_ref_id():
 ref_id = str(uuid.uuid4())[:11].replace('-','').lower()
 try:
 id_exists = Sample.objecsts.get(ref_id = ref_id)
 get_ref_id()
 except:
 return ref_id


class Sample(models.Model):
 ref_id = models.CharField(max_length = 128, primary_key = True, default=
get_ref_id)
 email = models.EmailField(max_length=128,unique=True)


 def __unicode__(self):
 return self.email


class MobileNumber(models.Model):
 sample = models.ForeignKey(Sample)
 mobile_number = models.CharField(max_length=128)


*admin.py*
















*from django.contrib import adminfrom sample.models import Sample, 
MobileNumberclass MobileNumberInline(admin.StackedInline): model = 
MobileNumber extra = 1class SampleAdmin(admin.ModelAdmin): inlines = 
[MobileNumberInline]admin.site.register(Sample,SampleAdmin)*





When i try to insert a record from admin it shows the error, But when i 
tried only with parent table it works.When i came to add child table its 
not working.Its just shows "Please correct the error below".And then i 
tried to save through userinterface views(out of admin) it shows the 
following error "The inline foreign key did not match the parent instance 
primary key". 





-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/854f71c8-2b21-4184-a533-497f39b236d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: NoReverseMatch at /polls/1/results/

2015-05-09 Thread Daniel Roseman
On Saturday, 9 May 2015 05:05:00 UTC+1, James Schneider wrote:
>
> I agree that the typo is also an issue and should be fixed, but that 
> wouldn't result in the OP's error, since reverse() is complaining about a 
> 'detail' URL specifically. The typo would result in a similar error when 
> the result page is displayed, and would show 'guestion' as one of the 
> kwargs.
>
> -James 
>
No, it wouldn't, and the typo is the cause of the error. 

The error occurs when the results page is rendered. That page has a link to 
the question. The url tag is trying to render that link: `{% url "detail" 
question_id=question.id %}`. But there is no `question` variable in the 
context, only `guestion`, so the question_id parameter to that URL tag is 
blank. Hence the error.
--
DR.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/eb5bb7ae-b87b-45a0-9107-46ecb18f1d25%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Defining base templates for reusable apps

2015-05-09 Thread Bruno A.
Hi,

I've never done it, but I myself want to do a similar thing, so I've been 
thinking about it a bit. The solutions I've thought of are the following:

   - Template inheritance, as you mention. You provide the base template, 
   but it might not be straightforward for the app's user to override. The 
   Django's admin is a good example of this, and it can be customized 
   partially or entirely by adding an admin directory in your site's templates 
   directory, there are also some apps providing new interfaces 
    entirely.
   - Use the include tag 
    
   to let your app's user define the main site canvas and just generate some 
   basic HTML in the middle of their page, with their navbar, footer...
   - Along the same lines, provide in your apps custom template tags for 
   the CSS, the JS you need and the content of your pages.

Now, I guess it depends on the complexity of the content your app provide, 
whether you need parameters, etc... 

On Friday, 8 May 2015 22:00:29 UTC+1, Some Developer wrote:
>
> I'm looking into building a set of open source reusable apps for Django 
> and was wondering what the currently accepted best practice was for 
> defining base templates for reusable apps was? 
>
> I want my reusable apps templates to be easy to slot into an already 
> existing site design but I'm unsure of the best way of achieving that. 
> Should I define a base template in the app itself that basically works 
> on the whole of the app? If I did that what would be the best way of 
> letting other people modify that base template? 
>
> I really just want my app to fit into other users projects with the 
> minimum amount of fuss. 
>
> I guess I could specify the views myself and let the user create the 
> templates themselves. Would that be a decent option? That would allow 
> the user to design the templates in the best possible way. 
>
> Having said that I would also like to provide some default templates 
> that people could use if they didn't want to bother designing the 
> templates themselves. How could I allow them to override the default 
> template supplied by the view? 
>
> Any hints would be greatly appreciated :). 
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/90cbc6eb-304a-4396-9155-597a9f75879e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Two types of users, how would I authenticate them?

2015-05-09 Thread charles javelona
Hi there, 

I am a newbie in Django. My goal is to create an online platform exchange 
for my community that helps students like me get part-time jobs from 
employers.
For the past days, I taught myself how to build a blog and the polls app, 
but it seems what I am building is more complex than a regular tutorial.

My first few obstacles are the following:
  - Registering and authenticating two types of users, Employers and 
Students.
  - Logging two the two types of users.
  - How would I even start defining the models for this?


Here is my first attempt of defining them.
from django.db import models

class User(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
email = models.EmailField(max_length=254, unique=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
mobile = models.CharField(max_length=12)
password = models.CharField(max_length=100, null=True)

def __str__(self):
return self.email

class Meta:
abstract = True


class Student(User):
school = models.CharField(max_length=40)


class Employer(User):
company = models.CharField(max_length=40)


Can someone please guide me in the right direction? I am fairly confuse and 
I do not want to abstract too much of the code because I want to truly 
understand how the Django architecure works.
If you need more information, feel free to ask me and I would be happy to 
clarify things.

Thanks a bunch!
Charles

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f926f8ee-4c5a-49d3-b157-7fcea43b53d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Two types of users, how would I authenticate them?

2015-05-09 Thread Tom Evans
On Sat, May 9, 2015 at 4:10 AM, charles javelona
 wrote:
> Hi there,
>
> I am a newbie in Django. My goal is to create an online platform exchange
> for my community that helps students like me get part-time jobs from
> employers.
> For the past days, I taught myself how to build a blog and the polls app,
> but it seems what I am building is more complex than a regular tutorial.
>
> My first few obstacles are the following:
>   - Registering and authenticating two types of users, Employers and
> Students.
>   - Logging two the two types of users.
>   - How would I even start defining the models for this?
>
>
> Here is my first attempt of defining them.
> from django.db import models
>
> class User(models.Model):
> created_at = models.DateTimeField(auto_now_add=True)
> email = models.EmailField(max_length=254, unique=True)
> first_name = models.CharField(max_length=30)
> last_name = models.CharField(max_length=30)
> mobile = models.CharField(max_length=12)
> password = models.CharField(max_length=100, null=True)
>
> def __str__(self):
> return self.email
>
> class Meta:
> abstract = True
>
>
> class Student(User):
> school = models.CharField(max_length=40)
>
>
> class Employer(User):
> company = models.CharField(max_length=40)
>
>
> Can someone please guide me in the right direction? I am fairly confuse and
> I do not want to abstract too much of the code because I want to truly
> understand how the Django architecure works.
> If you need more information, feel free to ask me and I would be happy to
> clarify things.
>
> Thanks a bunch!
> Charles

I would simplify things, have just one type of user, who can be
associated with 1:N companies and 1:N schools. A user is an employer
if they are associated with at least one company, and a user is a
student if they are associated with at least one school.

You could denormalise the relationship with attributes on the user
model if/when you are worried about performance.

Eg:

class Company(models.Model):
  name = ...
  contact_details = ...

class School(models.Model):
  name = 

class User(models.Model):
  ...
  company = models.ManyToManyField(Company)
  school = models.ManytoManyField(School)


You could expand things about the relationship by adding a information
to the join (or "through") table; perhaps you might want to record
when a student started/left a school; this would also allow you to
record which employers are alumni of a school:

https://docs.djangoproject.com/en/1.8/topics/db/models/#intermediary-manytomany

Cheers

Tom

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1KUBq5KAV50f%2Bxk0vzuxAsb7hLC5BNht_p6Vu8L3bgucA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Two types of users, how would I authenticate them?

2015-05-09 Thread Tim Chase
On 2015-05-09 15:52, Tom Evans wrote:
> I would simplify things, have just one type of user, who can be
> associated with 1:N companies and 1:N schools. A user is an employer
> if they are associated with at least one company, and a user is a
> student if they are associated with at least one school.
> 
> You could denormalise the relationship with attributes on the user
> model if/when you are worried about performance.
> 
> Eg:
> 
> class Company(models.Model):
>   name = ...
>   contact_details = ...
> 
> class School(models.Model):
>   name = 
> 
> class User(models.Model):
>   ...
>   company = models.ManyToManyField(Company)
>   school = models.ManytoManyField(School)

Since people can attend multiple schools and work for multiple
employers, you can just make it a many-to-many, using the stock
auth User:

  from django.contrib.auth.models import User

  class Company(models.Model):
...

  class School(models.Model):
...

  class Employee(models.Model):
company = models.ForeignKey(Company)
employee = models.ForeignKey(User)

  class Student(models.Model):
school = models.ForeignKey(School)
employee = models.ForeignKey(User)

-tkc



-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/20150509101555.4f0e8a50%40bigbox.christie.dr.
For more options, visit https://groups.google.com/d/optout.


Re: Two types of users, how would I authenticate them?

2015-05-09 Thread Tom Evans
On Sat, May 9, 2015 at 4:15 PM, Tim Chase
 wrote:
> Since people can attend multiple schools and work for multiple
> employers, you can just make it a many-to-many, using the stock
> auth User:
>
>   from django.contrib.auth.models import User
>
>   class Company(models.Model):
> ...
>
>   class School(models.Model):
> ...
>
>   class Employee(models.Model):
> company = models.ForeignKey(Company)
> employee = models.ForeignKey(User)
>
>   class Student(models.Model):
> school = models.ForeignKey(School)
> employee = models.ForeignKey(User)
>

This would just be explicitly defining the join or through tables used
for a M2M relationship, and is unnecessary if you are not adding
fields to the join table - Django can infer those tables for itself.

If you aren't adding an extra information to the join table, it would
still be better to specify them as a ManyToManyField, so that django
can provide a few extra bits of magic.

Django does not care what model you place the M2M relation on, so
without modifying the stock User model...

  class Company(models.Model):
 employees = models.ManyToManyField(User)

  class School(models.Model):
 students = models.ManyToManyField(User)


If you then wanted to define extra attributes on the relationship, eg
for a student, date started or date left...

  class Student(models.Model):
  school = models.ForeignKey(School)
  user = models.ForeignKey(User)
  enrolled_date = models.DateField(blank=True, null=True)
  graduation_date = models.DateField(blank=True, null=True)

  class School(models.Model):
 students = models.ManyToManyField(User, through=Student)

etc

It is always worth defining that a join table is part of a M2M
relationship, eg with a School and a User, you could do:

  some_school.students.add(some_user)

Without defining it as a M2M relationship, you would have to say:

  Student.objects.create(school=some_school, user=some_user)

which not only is less clear, but is also more typing!

Cheers

Tom

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1K%2Bt42KG6DNGYaSU4Pr5NW4_x0pgiFsV5oz4CdjtpTVTQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: NoReverseMatch at /polls/1/results/

2015-05-09 Thread James Schneider
>
> I agree that the typo is also an issue and should be fixed, but that
>> wouldn't result in the OP's error, since reverse() is complaining about a
>> 'detail' URL specifically. The typo would result in a similar error when
>> the result page is displayed, and would show 'guestion' as one of the
>> kwargs.
>>
>> -James
>>
> No, it wouldn't, and the typo is the cause of the error.
>
> The error occurs when the results page is rendered. That page has a link
> to the question. The url tag is trying to render that link: `{% url
> "detail" question_id=question.id %}`. But there is no `question` variable
> in the context, only `guestion`, so the question_id parameter to that URL
> tag is blank. Hence the error.
> --
> DR.
>
>
Yep, you're right. I hadn't thought far enough back in my own train of
thought. For some reason I had assumed that the OP just had {% url 'detail'
%} somewhere in a template (probably because I had just run into a similar
situation on a project), forgetting that the template system throws out
invalid context variable references.

Thanks for the correction.

-James

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXp6n8yqosHaLKQhhnLwpvuCVJ3LA7LRuUmiL0%3DDAOE_A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.